Forum: Ruby on Rails map.resources with habtm

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31c0dbdf2d27669a82671e9a0e3c571c?d=identicon&s=25 Martin Evans (martinevans)
on 2007-08-01 23:16
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
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-08-02 07:22
(Received via mailing list)
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...@andreas-s.net>
31c0dbdf2d27669a82671e9a0e3c571c?d=identicon&s=25 Martin Evans (martinevans)
on 2007-08-02 21:22
That's great Robert thank's very much

Martin

Robert Walker wrote:
> 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
This topic is locked and can not be replied to.