模型
1 个带有 sales_invoice_date、you_ref 等字段的平面发票表
控制器
def raw
@invoices = Invoice.find(:all, :order => 'sales_invoice_date, id')
@invoice_days = @invoices.group_by { |day| day.sales_invoice_date }
end
看法
<% @invoice_days.sort.each do |day, invoices| %>
<h2><%= day.to_date.to_s(:long) %></h2>
<% for invoice in invoices %>
<div class="invoice_line">
<strong><%= invoice.your_ref %></strong>
Sales invoice date: <%= invoice.sales_invoice_date.to_date.to_s(:long) %>
</div>
<% end %>
<% end %>
这让我的目标实现了一半。它打印以下内容:
September 9, 2012
2222 Sales invoice date: September 9, 2012
5555 Sales invoice date: September 9, 2012
2222 Sales invoice date: September 9, 2012
October 1, 2012
1111 Sales invoice date: October 1, 2012
1111 Sales invoice date: October 1, 2012
1111 Sales invoice date: October 1, 2012
November 11, 2012
3333 Sales invoice date: November 11, 2012
December 12, 2012
0000 Sales invoice date: December 12, 2012
我需要的是一种 group_by 并在日期循环中根据唯一的“your_ref”值再次排序的方法,它看起来像这样:
September 9, 2012
2222
2222 Sales invoice date: September 9, 2012
2222 Sales invoice date: September 9, 2012
5555
5555 Sales invoice date: September 9, 2012
October 1, 2012
1111
1111 Sales invoice date: October 1, 2012
1111 Sales invoice date: October 1, 2012
1111 Sales invoice date: October 1, 2012
November 11, 2012
3333
3333 Sales invoice date: November 11, 2012
December 12, 2012
0000
0000 Sales invoice date: December 12, 2012
谢谢