Ruby on rails 3 Rails 3应用程序中的记录合计和分组

Ruby on rails 3 Rails 3应用程序中的记录合计和分组,ruby-on-rails-3,Ruby On Rails 3,我在一个视图中使用它: <% @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_

我在一个视图中使用它:

 <% @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 %>

// 

我有一组具有:月、:年和:数量属性的行项目。我可以用上面的代码按月份和年份对它们进行分组,我也尝试按月份合计数量。我似乎不明白。获取以下错误:

undefined method `lines' for #<#<Class:0x007fabb4f8afe0>:0x007fabb4f806d0>
未定义的方法“行”#

您似乎在调用行,好像它是一个方法,而实际上它是一个对象数组。试着这样做:

<%= lines.map(&:quantity).sum %>

Map将通过对数组中的每条记录调用
quantity
来创建一个新数组。然后你可以把它加起来