Group by attributes

I’m thinking this is a pretty simple issue, but I’ve been staring at my
computer for too long :slight_smile:

I’m simply trying to generate a page that lists items grouped by their
“date” attribute like so:

09/25/06
Item1
Item3
09/26/06
Item2
Item4

I’m assuming I would generate some sort of hash in the controller that
looks like @things = { “09/25/06” => { Item1, Item3 }, “09/26/06” => {
Item2, Item4 } }
and then do something like this in the view:

<% for $thing in $things %>
.
.
.

But I can’t get any further than that. Is this the right direction? If
so, how would I generate the hash?

Thanks!

<% for $thing in $things %>

I meant <% for thing in @things %> , of course

Matt R. wrote:

<% for $thing in $things %>

I meant <% for thing in @things %> , of course


Posted via http://www.ruby-forum.com/.

try this…

groups = @things.group_by {|x| x.date}

groups.inspect #=> { ‘date1’ => [ array of items with date1], ‘date2’
=> [array of items with date2] }

_Kevin