# FOR loop.... has many belongs to?

**URL:** <https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034>\
**Category:** Rails\
**Created:** [April 16, 2010, 12:39am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034 "2010-04-16T00:39:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 12:39am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/1 "2010-04-16T00:39:48Z")

</div>

Hey,

I followed Ryan B. tutorials here about implemting checkboxes with  
has and belongs to many relationships.

> **[\#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17-habtm-checkboxes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

In case you dont have time to watch it, heres a quick summary–

* * *

Products and categories have the has and belongs to many relationship  
between them.

Then, in the products form, you can put this for creating check boxes,  
the ultimate goal of assigning this product with many categories–

\<% for category in Category.find(:all) %\>

 \<%= check\_box\_tag "product[category\_ids][]", category.id, @product.categories.include?(category) %\> \<%= category.name %\>
\<% end %\>
* * *

Ok great, that works.

the checkboxes all submit. It works great.

But my question is–

How can I display what categories a certain product belongs to? Most  
likely using a FOR loop, but I’m not too sure how to make one that  
displays the categories of a product.

Thanks, and feel free to ask more questions if u dont understand 🙂  
thanks

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 1:08am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/2 "2010-04-16T01:08:29Z")

</div>

\<%= @product.categories.map(&:name).to\_sentence %\>

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 1:43am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/3 "2010-04-16T01:43:37Z")

</div>

Hmmm thanks for the reply… but nothing is showing up. no error  
message though… is there a way to do this with a for loop?

I dont understand that peice of code… im a rails noobie. could you  
breifly explain? Thanks

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 2:11am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/4 "2010-04-16T02:11:08Z")

</div>

This is saying that a product has many categories. The category name  
will map to an array then the .to\_sentence outputs each item as  
1,2,3. nicely.

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 3:49am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/5 "2010-04-16T03:49:31Z")

</div>

Thanks, but are you sure this works with has and belongs to many?

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 4:01am UTC](https://www.ruby-forum.com/t/for-loop-has-many-belongs-to/187034/6 "2010-04-16T04:01:13Z")

</div>

If you have a product instance you can do @product.categories correct  
which returns an array of categories?
