# Count by group optimization

**URL:** <https://www.ruby-forum.com/t/count-by-group-optimization/109849>\
**Category:** Rails\
**Created:** [July 19, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/count-by-group-optimization/109849 "2007-07-19T16:57:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@Solid](https://www.ruby-forum.com/u/Solid)\
**Post date:** [July 19, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/count-by-group-optimization/109849/1 "2007-07-19T16:57:00Z")

</div>

Hello RoR people,

First time here.

I have a question about calculations in rails.

For example i have two tables:

categories  
id  
name

and questions:  
id  
category\_id  
name

How I can count questions in each category?

Please don’t give me recepies like: :include =\> [:questions] or  
something like: @categories.each { |c| c.questions.count } Because  
those methods is not so much optimized, to run them on a production  
server.

Thank you,  
Dmitry for Estonia.

---

<div class="post-metadata">

**Author:** ![Solid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@Solid](https://www.ruby-forum.com/u/Solid)\
**Post date:** [July 19, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/count-by-group-optimization/109849/2 "2007-07-19T18:03:37Z")

</div>

Solid wrote:

> Please don’t give me recepies like:

My company uses “recepies” like that on production servers. Could you  
be more specific? Which methods are you against using and why? Would  
you prefer writing your own SQL queries? Have you looked at the  
find\_by\_sql and count\_by\_sql methods?

---

<div class="post-metadata">

**Author:** ![Solid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@Solid](https://www.ruby-forum.com/u/Solid)\
**Post date:** [July 20, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/count-by-group-optimization/109849/3 "2007-07-20T18:23:03Z")

</div>

Solid wrote:

> Hello RoR people,
> 
> First time here.
> 
> I have a question about calculations in rails.
> 
> For example i have two tables:
> 
> categories  
> id  
> name
> 
> and questions:  
> id  
> category\_id  
> name
> 
> How I can count questions in each category?
> 
> Please don’t give me recepies like: :include =\> [:questions] or  
> something like: @categories.each { |c| c.questions.count } Because  
> those methods is not so much optimized, to run them on a production  
> server.

@category.questions.size

From the API docs:  
Returns the size of the collection by executing a SELECT COUNT(\*) query  
if the collection hasn’t been loaded and calling collection.size if it  
has. If it’s more likely than not that the collection does have a size  
larger than zero and you need to fetch that collection afterwards, it’ll  
take one less SELECT query if you use length.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
