# HABTM relationships in views

**URL:** <https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540>\
**Category:** rubyonrails-talk\
**Created:** [June 17, 2009, 12:55pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540 "2009-06-17T12:55:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 17, 2009, 12:55pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540/1 "2009-06-17T12:55:56Z")

</div>

Hi everyone. I'm currently working on a bodybuilding supplement Website which have 3 models :

\* Stack : has\_and\_belongs\_to\_many :products \* Product : has\_and\_belongs\_to\_many :stacks has\_and\_belongs\_to\_many :ingredients \* Ingredient : has\_and\_belongs\_to\_many :products

Now what I am trying to do is list all the ingredients for a Stack (using standard params[:id]). It seems that I cannot pass multiple product\_ids (but one id work fine!) to get the ingredients listing.

Here's a partial code from my controller :

=== START ===

&nbsp;&nbsp;def stack &nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@stack = Stack.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :template =\> "public/stack\_#{@lang}" &nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::RecordNotFound &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error("Attempt to access invalid stack #{params[:id]}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Désolé, cette formule est introuvable." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> :index &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

=== END ===

Any help would be appreciate and I can provide more information if my request isn't clear enought.

I am using RAILS\_GEM\_VERSION = '1.2.6'

Thanks

Hugues

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 17, 2009, 1:06pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540/2 "2009-06-17T13:06:17Z")

</div>

@stack.products will give you an array of products then for each product, product.ingredients will give you an array of ingredients.

Colin

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 17, 2009, 4:41pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540/3 "2009-06-17T16:41:27Z")

</div>

product.ingredients is an array, you could reference product.ingredients[0].name or you could iterate the array using product.ingredients.each. Another useful construct is product.ingredients.map(&:name) which will give you an array of ingredient names. In all of this don't forget to watch out for a product with no ingredients (product.ingredients is nil in this case) which can give you run-time errors if you have not caught them. The same applies to stack.products.

I wonder whether it would be useful for you to run through some basic Ruby tutorials to get a bit more grounding on the basics of Ruby.

Good Luck

Colin

---

<div class="post-metadata">

**Author:** ![Bill\_Walton1](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_Walton1](https://discuss.rubyonrails.org/u/Bill_Walton1)\
**Post date:** [June 17, 2009, 5:06pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540/4 "2009-06-17T17:06:29Z")

</div>

> product.ingredients is an array, you could reference

> In all of this don't forget to watch out for a product with no ingredients (product.ingredients is nil in this case)

I'm sure you meant to say that product.ingredients is an empty array

> I wonder whether it would be useful for you to run through some basic Ruby tutorials to get a bit more grounding on the basics of Ruby.

Excellent advice, put very nicely.

Best regards, Bill

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 17, 2009, 5:24pm UTC](https://discuss.rubyonrails.org/t/habtm-relationships-in-views/43540/5 "2009-06-17T17:24:14Z")

</div>

Colin Law wrote:
