I am setting up a many to many relationship using the :through method and want to test that I have set everything up correctly.
class MenuItem < ActiveRecord::Base
belongs_to :products #burger
belongs_to :additions #extra mustard
end
Products would be something like hamburger
class Product < ActiveRecord::Base
has_many :menu_items
has_many :additions, through: :menu_items
end
additions would be something like extra mustard or pickles
class Addition < ActiveRecord::Base
has_many :menu_items
has_many :products, through: :menu_items
end
I have set up my fixtures so that a hamburger should have 2 additions associated with it. Now I want to test that the association works. Not exactly sure how to do this. I tried this:
in the fixture I set hamburger to have an id of 22. also set the burger to have mustard and pickles (2 additions).
test "product 22 should have 2 additions associated with it" do
menu_item = Product.find(22).additions
assert_equal menu_item.count, 2
end
I get an uninitialized constants error
NameError: uninitialized constant Product::Additions
I am sure that I am just misunderstanding something. Would really appreciate any pointers.
The error is because you need to use singular for belongs_to.