Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:52:01+00:00 2026-06-02T05:52:01+00:00

I currently have 2 models setup: class Topic < ActiveRecord::Base belongs_to :category end class

  • 0

I currently have 2 models setup:

class Topic < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :topics
end

I am now trying to create a topic with a category associated in the rails console:

t = Topic.new :name => "Test", :category => Category.find(1)

Trouble is the model has category_id, and so I’d need to use:

c = Category.find(1)
t = Topic.new :name => "Test", :category_id => c.id

But, I’ve seen many times the ability to simply use :category instead of :category_id and pass in the category object instead of the objects id. Where am I going wrong?

When I do:

c = Category.find(1)
t = Topic.new :name => "Test", :category => c

I receive:

ActiveRecord::UnknownAttributeError: unknown attribute: category
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:52:03+00:00Added an answer on June 2, 2026 at 5:52 am

    You should be able to just do this:

    c = Category.find(1)
    t = Topic.new :name => "Test", :category => c
    

    The association definition on the model is what lets you do this.

    Interesting note, you can use :category_id and still just pass in the object, it will get the ID for you:

    t = Topic.new :name => "Test", :category_id => c
    

    Another way do do it which can be a bit nicer:

    t = c.topics.build(:name => "Test") # Builds an object without saving
    
    t = c.topics.create(:name => "Test") # Builds an object and saves it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following models setup: class Member < ActiveRecord::Base has_many :children end
I have three Models setup with the following associations class User < ActiveRecord::Base has_many
I currently have the following models/toy.rb file in my RoR project: class ToysPurchased include
I have two ActiveRecord models of the same class: #1: represents the current committed
In the Rails application I’m currently developing I have many request models. We are
I'm currently managing multiple models from a single SITE MANAGER page. I have the
I currently have a model set up like so: I have a Test class
I currently have a repository setup that loads forms a person would need to
I have a has_many through association setup between a song model and an artist
Given the following setup, I have three assemblies. Web (ASP.NET MVC 3.0 RC1) Models

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.