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

  • SEARCH
  • Home
  • 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 8910925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:08+00:00 2026-06-15T03:49:08+00:00

I have a many-to-many association between a Post and a Category model: categorization.rb: class

  • 0

I have a many-to-many association between a Post and a Category model:

categorization.rb:

class Categorization < ActiveRecord::Base
  attr_accessible :category_id, :post_id, :position

  belongs_to :post
  belongs_to :category
end

category.rb:

class Category < ActiveRecord::Base
  attr_accessible :name

  has_many :categorizations
  has_many :posts, :through => :categorizations  

  validates :name, presence: true, length: { maximum: 14 }
end

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :category_ids

  has_many :categorizations
  has_many :categories, :through => :categorizations  

  accepts_nested_attributes_for :categorizations, allow_destroy: true

end

This works:

post_spec.rb:

describe Post do

  let(:user) { FactoryGirl.create(:user) }
  let(:category) { FactoryGirl.create(:category) }
  before { @post = user.posts.build(title: "Lorem ipsum",
                                    content: "Lorem ipsum dolor sit amet",
                                    category_ids: category) }

My problem it’s here:

factories.rb:

  factory :post do
    title "Lorem"
    content "Lorem ipsum"
    category_ids category
    user
  end

  factory :category do
    name "Lorem"
  end

reply_spec.rb:

describe Reply do

  let(:post) { FactoryGirl.create(:post) }
  let(:reply) { post.replies.build(content: "Lorem ipsum dolor sit amet") }

When I run the test for reply_spec.rb I get this error:

> undefined method `category=' for #<Post:0x9e07564>

This is part that that is not working I think:

factories.rb:

  category_ids category

Am I defining the nested attribute in a wrong way? What’s the proper one?

  • 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-15T03:49:09+00:00Added an answer on June 15, 2026 at 3:49 am

    this post uses after_build hooks to create associations: Populating an association with children in factory_girl

    Personally I like not having factories too complicated (makes them too specific imo), and instead instantiate any neccessary associations in the tests as needed.

    factories.rb:

    factory :post do
      title "Lorem"
      content "Lorem ipsum"
      user
    end
    
    factory :category do
      name "Lorem"
    end
    

    post_spec.rb:

    ...
    let(:post) {FactoryGirl.create(:post, :category => FactoryGirl.create(:category))}
    

    (edit — since post object has associations to categorizations and not to categories directly)

    let(:post) {FactoryGirl.create(:post)}
    let(:categorization) {FactoryGirl.create(:categorization, 
                                      :post=> post, 
                                      :category=> FactoryGirl.create(:category))}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following are the models and association. class Vendor < ActiveRecord::Base attr_accessible :name, :address_attributes
I have a many-to-many association between 'Category' and 'Product' entities in my Entity Framework
I have a has_many :through association setup between two tables (Post and Category). The
I have a many-to-many association between a User class and a Table class. Additionally,
I have a Product and Category entity in many-to-many association. I am trying to
I have Product model and it has many categories with a has_many :through association
I have a many-to-many relationship defined between two entities using an association table and
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I am using Entity Framework 4. I have a many-to-many association (relationship) between two
I have two entities, Account and Subscription with a many-to-many association between them. I

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.