# Map.resources with habtm

**URL:** <https://www.ruby-forum.com/t/map-resources-with-habtm/111015>\
**Category:** Rails\
**Created:** [August 1, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/map-resources-with-habtm/111015 "2007-08-01T23:16:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [August 1, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/map-resources-with-habtm/111015/1 "2007-08-01T23:16:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [August 2, 2007, 7:22am UTC](https://www.ruby-forum.com/t/map-resources-with-habtm/111015/2 "2007-08-02T07:22:03Z")

</div>

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 E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [August 2, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/map-resources-with-habtm/111015/3 "2007-08-02T21:22:40Z")

</div>

That’s great Robert thank’s very much

Martin

Robert W. 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
