I’m using a standard .each loop to list the names of authors assigned to a contract for a particular project.
This works fine, when there’s just one author:
<% @product.contracts.each do |product| %>
<%= product.author.full_name %>
<% end %>
But more than one author and it looks really ugly. So I tried this:
<% @product.contracts.each do |p| %>
<%= p.author.full_name %>
<%= "," unless p == @product.last %>
<% end %>
But that didn’t work. I got: undefined method last for #<Product:0x105e7ac38>
Then, I tried this: <%= @product.map{|p| p.author.full_name}.join(",") %>
That didn’t work either. I got: undefined method map for #<Product:0x105c6d760>
I’m sure I’m doing something silly wrong, but I can’t see it.
You are trying to call these methods on a singular object, rather than an array of them(which support
lastandmap).@productis a model object rather than an array of them, which is what you are expecting.From your code, you want:
or
because contracts(assuming) is an array that responds to
mapandlast.