I’m using this in a view:
<% @line_items.group_by(&:year).each do |year, line_items| %>
<h2><%= year %></h2>
<% line_items.group_by(&:month).each do |month, lines| %>
<h3><%= Date.new(year.to_i, month.to_i, 1).strftime('%B') %> // <%= lines(&:quantity).sum %></h3>
<% for line in lines %>
<p><%= line.quantity %> <%= line.customer.name %></p>
<% end %>
<% end %>
<% end %>
I have a group of line items that have a :month, :year and :quantity attribute. I can group them by month and year with the above code, by I’m trying to total the quantity by month, too. Can’t seem to figure that out. Getting the following error:
undefined method `lines' for #<#<Class:0x007fabb4f8afe0>:0x007fabb4f806d0>
You seem to be calling lines as if it were a method when in fact it is an array of objects. Try to do it like this instead:
Map will create a new array by calling
quantityon each record in the array. Then you can sum it.