Using "join" on associated objects


#1

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.


#2

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') %>

#3

On Mar 3, 8:07 pm, partydrone 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


#4

On Tue, Mar 3, 2009 at 2:07 PM, partydrone 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/