Search code examples
rubyormruby-datamapper

Group DataMapper records, then sort within those groups


I have records that I'd like to sort like this:

         Status       Date
Frank   STATUS_A   Jan 1, 2013
Jenny   STATUS_A   Feb 2, 2013
Alice   STATUS_A   Mar 3, 2013
Bob     STATUS_B   Jan 1, 2013
Zeke    STATUS_B   Feb 12, 2013
Harry   STATUS_B   Apr 4, 2013

Group together by Status and then sort by Date within each group.

Cannot figure out how to do this in ruby DataMapper.

I tried something like:

Person.all(:fields => [:status], :unique => true, :order => [:date.asc])

But got:

DataMapper::ImmutableError - Immutable resource cannot be lazy loaded

I also tried:

Person.all(:order => [:status]).all(:order => [:date])

This does the initial status grouping fine, but the second date ordering isn't constrained within each status group.


Solution

  • Hope this works:

    Person.all(:order => [:status, :date])