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:


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?


<% 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

try this…

groups = @things.group_by {|x|}

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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs