开发者

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| %>
 开发者_如何学JAVA    <%=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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜