Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - when using Group_by - How to get an Index?

I have the following:

sets = DataSet.all.group_by{ |data| [data.project_id, "-", data.thread_id].join(" ") }

<% sets.each do |range, datas| %>
  <p><%= range %>:</p>
  <% datas.each do |data| %>
     <%=data%>
    <p>Last Post<%= data.last.created_at %></p>
  <% end %>
<% end %>

Problem is that I need an index. So i updated the above with:

<% sets.each_with_index do |range, datas, i| %>
  <p><%= range %>:</p>
  <% datas.each do |data| %>
     <%= i %>
     <%=data%>
    <p>Last Post<%= data.last.created_at %></p>
  <% end %>
<% end %>

That then breaks, with the error: undefined method `last' for 0:Fixnum

Ideas? thank you

like image 944
AnApprentice Avatar asked Feb 12 '11 22:02

AnApprentice


1 Answers

The issue you observe is because of the way parameters are assigned to the block. In your second example, you will observe that range contains an array containing a single range and the matching datas, the datas variable contains the index and i is always nil.

This is because ruby "unsplats" arrays if it is the only parameter to the block. If you have more than one type (in this case an array and an integer), you must hint ruby on what it should do. The simplest way is to use parentheses.

<% sets.each_with_index do |(range, datas), i| %>
  ...
<% end %>

That way, ruby will know what you mean and split the array up into range and datas. This is actually a feature of ruby's assignment operator in conjunction with the comma operator. It works like this

my_array = [1, 2]
(x, y) = my_array
p x                # prints 1
p y                # prints 2
like image 67
Holger Just Avatar answered Sep 20 '22 23:09

Holger Just