I’m attempting to create a has_many :through relationship for my classes, but am confused about how to actually implement the saving and retrieval of the related data.
I have a Recipe class, Ingredient class, and Amount class that is the :through of the associations. Here are my classes as they stand now:
#models/recipe.rb
class Recipe < ActiveRecord::Base
has_many :amounts
has_many :ingredients, :through => :amounts
end
#models/ingredient.rb
class Ingredient < ActiveRecord::Base
has_many :amounts
has_many :recipes, :through => :amounts
end
#models/amounts.rb
class Amounts < ActiveRecord::Base
belongs_to :recipes
belongs_to :ingredients
end
Also, here is a link to a Gist of the rudimentary form for creating Recipes: https://gist.github.com/2820455
And the Gist for the creation code itself:
https://gist.github.com/2820294
When I submit the form, I get this error:
uninitialized constant Recipe::Amount
and this app trace:
app/controllers/recipes_controller.rb:57:in 'block in create'
app/controllers/recipes_controller.rb:56:in 'create'
I’m extremely new to Ruby on Rails and implementing this relationship and getting the Amounts values inserted into the table is boggling my mind…!
belongs_to needs to be singular, in your amount.rb change it to look like this :-
models/amounts.rb