# Using "join" on associated objects

**URL:** <https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016>\
**Category:** rubyonrails-talk\
**Created:** [March 3, 2009, 8:07pm UTC](https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016 "2009-03-03T20:07:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Party\_Drone](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Party\_Drone](https://discuss.rubyonrails.org/u/Party_Drone)\
**Post date:** [March 3, 2009, 8:07pm UTC](https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016/1 "2009-03-03T20:07:21Z")

</div>

I have a model, Download, with a HABTM relationship to another model, Product. In the index view for Download, I want to list all of the associated products by name. Here is my view code for the table row:

\<script src="[http://gist.github.com/73492.js&quot;&gt;&lt;/script&gt](http://gist.github.com/73492.js&quot;&gt;&lt;/script&gt);

I've tried using "download.products.join(', ')", but all I get is the objects has mark (#) in my HTML. Is there a quick and easy way of creating a joined list of all the product names that I just don't know about?

Thanks.

---

<div class="post-metadata">

**Author:** ![Party\_Drone](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Party\_Drone](https://discuss.rubyonrails.org/u/Party_Drone)\
**Post date:** [March 3, 2009, 8:09pm UTC](https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016/2 "2009-03-03T20:09:02Z")

</div>

Sorry, here's my view code:

\<tr class="\<%= cycle('off', 'on') %\>"\> &nbsp;&nbsp;\<td\>\<%= link\_to(download.title, edit\_download\_path(download)) %\>\</td\> &nbsp;&nbsp;\<td\>\<%= link\_to(download.file\_name, download\_path(download)) %\>\</td\> &nbsp;&nbsp;\<td\>\<%= download.part\_number %\>\</td\> &nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;\<% download.products.each do |product| -%\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= product.name %\>, &nbsp;&nbsp;&nbsp;&nbsp;\<% end -%\> &nbsp;&nbsp;\</td\> &nbsp;&nbsp;\<td class="delete"\>\<%= link\_to('&times;', download\_path (download), :confirm =\> "Are you sure you want to delete # {download.title}?", :method =\> 'delete') %\>\</td\> \</tr\>

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 3, 2009, 8:12pm UTC](https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016/3 "2009-03-03T20:12:27Z")

</div>

> I have a model, Download, with a HABTM relationship to another model, Product. In the index view for Download, I want to list all of the associated products by name. Here is my view code for the table row:
> 
> \<script src="[http://gist.github.com/73492.js&quot;&gt;&lt;/script&gt](http://gist.github.com/73492.js&quot;&gt;&lt;/script&gt);
> 
> I've tried using "download.products.join(', ')", but all I get is the objects has mark (#) in my HTML. Is there a quick and easy way of creating a joined list of all the product names that I just don't know about?

when you call join on products it calls to\_s on each Product, because it doesn't know any better (and the particular details of the default to\_s on objects means that what you get is invalid html that is often displayed as just # by browers). It's up to you to turn that array of products into an array of suitable strings (checkout the select/map methods on Array) and then call join on that

Fred

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [March 4, 2009, 2:34am UTC](https://discuss.rubyonrails.org/t/using-join-on-associated-objects/40016/4 "2009-03-04T02:34:49Z")

</div>

\<%= @download.products.collect{ |p| p.name }.join( ', ' ) %\>
