# Include grouped count in result?

**URL:** <https://www.ruby-forum.com/t/include-grouped-count-in-result/145688>\
**Category:** Rails\
**Created:** [August 13, 2008, 12:02am UTC](https://www.ruby-forum.com/t/include-grouped-count-in-result/145688 "2008-08-13T00:02:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 12:02am UTC](https://www.ruby-forum.com/t/include-grouped-count-in-result/145688/1 "2008-08-13T00:02:47Z")

</div>

Hi, I am trying to write a method or named\_scope that will return me  
things based on a grouping… AND I would like to see how many are in  
that group.

I’ve got this:

named\_scope :top, :group =\> “view\_type, view\_id”, :order =\>  
“count(view\_id) desc”, :limit =\> 25

Which will (correctly) return me the things with the most rows in  
descending order. But I would love to know how MANY rows there were in  
each grouping. As if I were running this sql:

SELECT \*, count(view\_id) FROM `trackers` GROUP BY view\_type, view\_id  
ORDER BY count(view\_id) desc LIMIT 25  
which will return me:  
8 1 13 Genre 2008-08-12 18:54:42 2008-08-12 18:54:42 3 (\<- this is  
the  
number of rows)

How can I best do this?
