Martin,
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
actually different.
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_many :recipe_ingredients
has_may :ingrediants, :through => :recipe_ingredients
end
class Ingredient << ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, :through => :recipe_ingredents
end
class RecipeIngredient << ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
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_ingredients_controller.rb
-----------------------------------------------------------
def create
@recipe_ingredient = RecipeIngredient.new(params[:
recipe_ingredient])
...
...
end
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 Evans
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hi
> I''m trying to use restful resources with recipes and ingredients
>
> I''ve set up the models using scaffold_resource.
> A recipe can have many ingredients
> and an ingredient can be in many recipes.
> I''ve set the relationships up using a through.
> I now want to display a recipe with a list of ingredients but I''m
unsure
> how to proceed.
> Should I use nested resources and how do I add a new ingredient to a
> recipe when the link is stored in a different table. This is my first
> rails app, hence all the questions
> Thanks
> Martin
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---