模型
包含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并在日期循环中再次排序在唯一的&#34; your_ref&#34;值,它看起来像这样:
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
由于
答案 0 :(得分:3)
而不是
<% 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 %>
尝试:
<% invoices.group_by(&:your_ref).sort.each do |ref, ref_invoices| %>
<div class="invoice_line">
<strong><%= ref %></strong>
</div>
<% for invoice in ref_invoices %>
<strong><%= invoice.your_ref %></strong>
Sales invoice date: <%= invoice.sales_invoice_date.to_date.to_s(:long) %>
<% end %>
<% end %>
这也将通过参考
对发票进行分组