# Paginate and 'Group By' Option

**URL:** <https://www.ruby-forum.com/t/paginate-and-group-by-option/68333>\
**Category:** Rails\
**Created:** [September 11, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/paginate-and-group-by-option/68333 "2006-09-11T21:17:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [September 11, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/paginate-and-group-by-option/68333/1 "2006-09-11T21:17:48Z")

</div>

My SQL query also has a ‘group by’ option.

When I try to use paginate, I can find order option. But when I try to  
use

:group =\> ‘customer.id’

It shows that this is an unknown option. I found that paginate does not  
have group option.

Anyone has any idea how to add ‘group by customer.id’ into paginate.

Thank you very much for the help.

---

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [September 12, 2006, 7:38am UTC](https://www.ruby-forum.com/t/paginate-and-group-by-option/68333/2 "2006-09-12T07:38:03Z")

</div>

use the paginating\_find plugin (read about it on  
[http://cardboardrocket.com](http://cardboardrocket.com)).  
That being said, the count will be wrong so you will need to hack the  
find  
method to help it figure out how many pages are in the collection.  
Here’s  
the hack:

Find the code in paginating\_find.rb that says:

total\_size = limit ? limit : count(options[:conditions],  
options[:joins])

Replace it with:

```
    if block_given?
      total_size = limit ? limit : yield
    else
      total_size = limit ? limit : count(options[:conditions],

```

options[:joins])  
end

Now, when you call find, do it as:

@my\_fine\_stuff = Model.find(:all, …whateveroptionsyouhave…) {  
whatever  
it takes to count the records in the collection }

It’s not too economical of SQL queries but it works.

Bryan Roxah wrote:

> have group option.

–  
View this message in context:  
[http://www.nabble.com/Paginate-and-'Group-By'-Option-tf2254447.html#a6259843](http://www.nabble.com/Paginate-and-%27Group-By%27-Option-tf2254447.html#a6259843)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
