我正在尝试创建信息的交叉表。我可以按日期分组,但我很难按单位代码对它们进行总计。我也在努力使视图正确显示。我想在列中显示日期而不是显示为行。我使用this作为总和的指导。我也看了this,但老实说,这有点过头了。
样本数据
|Productiondate| Unit Code | Quantity
| 2017-1-1 | Part 1 | 2 |
| 2017-1-1 | Part 1 | 3 |
| 2017-1-22 | Part 1 | 1 |
| 2017-1-1 | Part 2 | 3 |
| 2017-1-10 | Part 2 | 1 |
| 2017-1-22 | Part 2 | 1 |
最终结果表
|Unit Code | Week 1 | Week 2 | Week 3
-------------------------------------
| Part 1 | 5 | 0 | 1 |
| Part 2 | 3 | 1 | 1 |
reportcontroller.rb
def workorder_production
#@productions = Production.all
#@production_weeks = @productions.group_by { |t| t.created_at.beginning_of_week }
productions = Production.select(:unitcode, :created_at, sum(:quantity).group_by { |t| t.created_at.beginning_of_week })
end
上面评论的行可以使用,但不会累计数量当我尝试取消注释部分时
NoMethodError未定义方法`sum'
workorder_production.html.erb
<% @production_weeks.sort.each do |week, productions| %>
<h2>Week Number <%= week.strftime('%U') %></h2>
<% for production in productions %>
<div class="production">
<p>Unit Code: <%= production.unitcode %> Qty: <%= production.quantity %></p>
</div>
<% end %>
<% end %>
谢谢!