Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
paginate a collection passed through group_by
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Danimal  
View profile  
 More options Aug 16 2009, 4:15 pm
From: Danimal <fightonfightw...@gmail.com>
Date: Sun, 16 Aug 2009 13:15:46 -0700 (PDT)
Local: Sun, Aug 16 2009 4:15 pm
Subject: paginate a collection passed through group_by
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.

Help?

-Danimal


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mislav Marohnić  
View profile  
 More options Aug 18 2009, 9:33 am
From: Mislav Marohnić <mislav.maroh...@gmail.com>
Date: Tue, 18 Aug 2009 15:33:44 +0200
Local: Tues, Aug 18 2009 9:33 am
Subject: Re: paginate a collection passed through group_by

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philipp Pirozhkov  
View profile  
 More options Jul 4 2012, 6:49 am
From: Philipp Pirozhkov <pirjs...@gmail.com>
Date: Wed, 4 Jul 2012 03:49:34 -0700 (PDT)
Local: Wed, Jul 4 2012 6:49 am
Subject: Re: paginate a collection passed through group_by

You're doing it wrong:
User.group('role').sum('age').paginate

Use
User.group('role').paginate.sum('age')
instead


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »