Model
1 flat invoices table with sales_invoice_date, you_ref amongst other fields
Controller
def raw
@invoices = Invoice.find(:all, :order => 'sales_invoice_date, id')
@invoice_days = @invoices.group_by { |day| day.sales_invoice_date }
end
View
<% @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 %>
This gets me halfway to my objective. It prints the following:
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
What I need is a way to group_by and sort again within the date loop on unique “your_ref” values, it would look something like this:
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
Thanks
Instead of
try:
This will also group invoices by ref.