0

I have the following setup

class Category < ActiveRecord::Base 

has_many :category_products
has_many :products, through: :category_products

end

class Product < ActiveRecord::Base

has_many :category_products
has_many :categories, through: :category_products

end

class CategoryProducts < ActiveRecord::Base
belongs_to :category
belongs_to :product
end

In rails console I try allocate category_ids using the following command p.category_ids = [1,2] (where p = Product.first) I get

NameError: uninitialized constant Product::CategoryProduct

any advice?

thanks

Mark Soden
  • 81
  • 9
  • turns out, rails doesn't like a 'multi' name for the joining model, created a new model called categorization and all works 100% – Mark Soden Oct 05 '12 at 18:47

2 Answers2

0

turns out, rails doesn't like a 'multi' name for the joining model, created a new model called categorization and all works 100%

Mark Soden
  • 81
  • 9
0

You just make a typo. try this:

class Category < ActiveRecord::Base 
  has_many :categories_products
  has_many :products, through: :categories_products
end

class Product < ActiveRecord::Base
  has_many :categories_products
  has_many :categories, through: :categories_products
end

class CategoriesProduct < ActiveRecord::Base # singular association model define
  belongs_to :category
  belongs_to :product
end

careful about the association name.

model: categories_product(no s), and it's table categories_products

ruohanc
  • 182
  • 1
  • 8