In you subject line you say you are using has_and_belongs_to_many
(habtm) and in the body of your post you say you are using through. I
would first suggest that you don’t confuse these two since they are
I am going on the assumption that you have created a many-to-many
using has_many :through.
So you would have something like this:
class Recipe << ActiveRecord::Base
has_may :ingrediants, :through => :recipe_ingredients
class Ingredient << ActiveRecord::Base
has_many :recipes, :through => :recipe_ingredents
class RecipeIngredient << ActiveRecord::Base
I would assume that a RecipeIngredient would have an additional column
to store the quantity of the ingredient for the recipe.
Now you question is about adding ingredients to a recipe. Well in
this case try to think about it as a simple CRUD operation. In
thinking about it this way you would “Create” a new RecipeIngredient
and assign it to a recipe and to an ingredient with something along
the line of:
@recipe_ingredient = RecipeIngredient.new(params[:
Now here’s the tricky part. When thinking about your RESTful style
model names it would be nice to come up with a better name for the
join table than RecipeIngredient. Unfortunately, I don’t have any
good suggestions off the top of my head. But, an example would be a
many-to-many between Person and Group. A good name for the model in
that case would be Membership. A person could be a member of multiple
groups and each group would have multiple people. In any case try to
give your join model some meaningful name (meaningful as it relates to
CRUD that is). Your join model should be something that makes sense
to create the relationship, read items in the relationship, update the
relationship or delete the relationship.
Hope that help.
On Aug 1, 5:16 pm, Martin E. firstname.lastname@example.org