# Using "join" on associated objects

**URL:** <https://www.ruby-forum.com/t/using-join-on-associated-objects/161921>\
**Category:** Rails\
**Created:** [March 3, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/using-join-on-associated-objects/161921 "2009-03-03T21:08:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 3, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/using-join-on-associated-objects/161921/1 "2009-03-03T21:08:09Z")

</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:

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:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 3, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/using-join-on-associated-objects/161921/2 "2009-03-03T21:09:56Z")

</div>

Sorry, here’s my view code:

 \<%= link\_to(download.title, edit\_download\_path(download)) %\> \<%= link\_to(download.file\_name, download\_path(download)) %\> \<%= download.part\_number %\> \<% download.products.each do |product| -%\> \<%= product.name %\>, \<% end -%\> \<%= link\_to('×', download\_path (download), :confirm =\> "Are you sure you want to delete # {download.title}?", :method =\> 'delete') %\>

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 3, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/using-join-on-associated-objects/161921/3 "2009-03-03T21:13:29Z")

</div>

On Mar 3, 8:07 pm, partydrone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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:
> 
> 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:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 4, 2009, 3:35am UTC](https://www.ruby-forum.com/t/using-join-on-associated-objects/161921/4 "2009-03-04T03:35:22Z")

</div>

On Tue, Mar 3, 2009 at 2:07 PM, partydrone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

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

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
