Forum: Ruby on Rails Using "join" on associated objects

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
829f2eef24c244ea33165adde8bd36d5?d=identicon&s=25 partydrone (Guest)
on 2009-03-03 21:08
(Received via mailing list)
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"></script>

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.
829f2eef24c244ea33165adde8bd36d5?d=identicon&s=25 Party Drone (Guest)
on 2009-03-03 21:09
(Received via mailing list)
Sorry, here's my view code:

<tr class="<%= cycle('off', 'on') %>">
  <td><%= link_to(download.title, edit_download_path(download)) %></td>
  <td><%= link_to(download.file_name, download_path(download)) %></td>
  <td><%= download.part_number %></td>
  <td>
    <% download.products.each do |product| -%>
    <%= product.name %>,
    <% end -%>
  </td>
  <td class="delete"><%= link_to('&times;', download_path
(download), :confirm => "Are you sure you want to delete #
{download.title}?", :method => 'delete') %></td>
</tr>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-03 21:13
(Received via mailing list)
On Mar 3, 8:07 pm, partydrone <partydr...@gmail.com> 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:
>
> <script src="http://gist.github.com/73492.js"></script>
>
> 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
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-04 03:35
(Received via mailing list)
On Tue, Mar 3, 2009 at 2:07 PM, partydrone <partydrone@gmail.com> 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 Donald
http://destiney.com/
This topic is locked and can not be replied to.