Complex nested group_by

Hello All,

I'm trying to determine an elegant way to do a nested group_by. What I would like to do is group records pulled from the database by one field and then another and display them in the view. Ideally it would look something like this:

Controller:

@groups = Record.all.group_by(&:field1).group_by(&:field2)

In the View:

<% @groups.each do | field1, group1 | %>   <% field1 %>     <% field2.each do | field2, group2 |       <% field2 %>       <% group2.each do |record| %>            <% record.name %>     <% end %> <% end %>

I've been able to do the second group_by in the view code, flatten it into and array and then loop through it, but that seems hackish. What I'm shooting for is to allow the user to pick two fields to group by in a form submit and then print out a report with the data grouped in that way.

Any thoughts?

Thanks, Nick