I'm trying to figure out how to paginate a collection that I run
through a "group_by" call (that's part of Rails, not the group_by SQL
syntax).
For example, let's say that I have 100 users in my system and I want
to list them, 10 to a page, and grouped by their state (US state, not
AASM state). So let's say that I have 7 users from Colorado, but
alphabetically, only two of them are in the first 10 returned.
If I paginate first, then group, I'll end up with page 1 showing CO
with 2 users, then some future page showing CO again. If I group
first, I'll have all 7 CO users grouped together, but then I don't
know how to paginate it? Cause let's say that instead of 7 there were
12 users from CO... it should show CO and the first 10, then on the
next page show CO and the last 2.
Has anyone else run into something similar? How do you handle it? I
know conceptually I want to group first then paginate, but the
grouping becomes a nested array (actually, it appears to be an
"ActiveSupport::OrderedHash") and paginate doesn't work on it:
>> grouped_users.class
=> ActiveSupport::OrderedHash
>> grouped_users.paginate
NoMethodError: undefined method `paginate' for
#<ActiveSupport::OrderedHash:0x34e5860>
from (irb):7
Is there a better way to think about this? If, for example, group one
has 6 users and group two has 7 users and I'm paginating 10 per page,
it should show "Group One" and have 6 users listed, then show "Group
Two" and have 4 users listed. Then the next page should show "Group
Two" and have the last 3 users listed, then "Group Three", etc.
On Sun, Aug 16, 2009 at 22:15, Danimal <fightonfightw...@gmail.com> wrote:
> If I paginate first, then group, I'll end up with page 1 showing CO > with 2 users, then some future page showing CO again. If I group > first, I'll have all 7 CO users grouped together, but then I don't > know how to paginate it?
Your problem is not that you want to paginate an OrderedHash; your problem is that you haven't decided exactly how this is supposed to look like in the user interface. Because you already have to load all the users from the database in order to perform the group_by, my suggestion is simply to forget about pagination and output them as-is.
Pagination is meant to avoid costly database operations, not to solve presentational issues.
On Monday, August 17, 2009 12:15:46 AM UTC+4, Danimal wrote:
> Hello!
> I'm trying to figure out how to paginate a collection that I run > through a "group_by" call (that's part of Rails, not the group_by SQL > syntax).
> For example, let's say that I have 100 users in my system and I want > to list them, 10 to a page, and grouped by their state (US state, not > AASM state). So let's say that I have 7 users from Colorado, but > alphabetically, only two of them are in the first 10 returned.
> If I paginate first, then group, I'll end up with page 1 showing CO > with 2 users, then some future page showing CO again. If I group > first, I'll have all 7 CO users grouped together, but then I don't > know how to paginate it? Cause let's say that instead of 7 there were > 12 users from CO... it should show CO and the first 10, then on the > next page show CO and the last 2.
> Has anyone else run into something similar? How do you handle it? I > know conceptually I want to group first then paginate, but the > grouping becomes a nested array (actually, it appears to be an > "ActiveSupport::OrderedHash") and paginate doesn't work on it:
> >> grouped_users.class > => ActiveSupport::OrderedHash > >> grouped_users.paginate > NoMethodError: undefined method `paginate' for > #<ActiveSupport::OrderedHash:0x34e5860> > from (irb):7
> Is there a better way to think about this? If, for example, group one > has 6 users and group two has 7 users and I'm paginating 10 per page, > it should show "Group One" and have 6 users listed, then show "Group > Two" and have 4 users listed. Then the next page should show "Group > Two" and have the last 3 users listed, then "Group Three", etc.