# HABTM relationships in views

**URL:** <https://www.ruby-forum.com/t/habtm-relationships-in-views/196971>\
**Category:** Rails\
**Created:** [June 17, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971 "2009-06-17T14:55:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/1 "2009-06-17T14: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 ===

def stack  
begin  
@stack = Stack.find(params[:id])  
render :template =\> “public/stack\_#{@lang}”  
rescue ActiveRecord::RecordNotFound  
logger.error(“Attempt to access invalid stack #{params[:id]}”)  
flash[:notice] = “DÃ©solÃ©, cette formule est introuvable.”  
redirect\_to :action =\> :index  
end  
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:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/2 "2009-06-17T15:07:03Z")

</div>

2009/6/17 Hugues B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> - Ingredient :  
> Â def stack  
> === END ===  
> @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:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/3 "2009-06-17T18:30:10Z")

</div>

Colin L. wrote:

> 2009/6/17 Hugues B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > - Ingredient :  
> > Â def stack  
> > === END ===  
> > @stack.products will give you an array of products then for each  
> > product, product.ingredients will give you an array of ingredients.
> 
> Colin

Thank you Colin, it seems to work since “#” character appear for each  
ingredient of a product. Now I must figure out how I get the  
ingredient’s name. Can you give me an exemple, since  
product.ingredients.name doesn’t work?  
Regards,  
Hugues

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/4 "2009-06-17T18:42:22Z")

</div>

2009/6/17 Hugues B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thank you Colin, it seems to work since “#” character appear for each  
> ingredient of a product. Now I must figure out how I get the  
> ingredient’s name. Can you give me an exemple, since  
> product.ingredients.name doesn’t work?

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:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/5 "2009-06-17T19:24:14Z")

</div>

Colin L. wrote:

> 2009/6/17 Hugues B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Thank you Colin, it seems to work since “#” character appear for each  
> > ingredient of a product. Now I must figure out how I get the  
> > ingredient’s name. Can you give me an exemple, since  
> > product.ingredients.name doesn’t work?
> 
> 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

Thank you Colin. I managed to get it right with your advice using

product.ingredients.each do |ingredient|

and then using

ingredient.name

I will do some Arrays and Hashes tutorials right away since I think I’m  
gonna need to manipulate data that way sooner or later.

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [June 17, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/habtm-relationships-in-views/196971/6 "2009-06-17T19:07:02Z")

</div>

On Wed, 2009-06-17 at 17:41 +0100, Colin L. wrote:

> 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
