# Dynamic Finders and Count

**URL:** <https://www.ruby-forum.com/t/dynamic-finders-and-count/118383>\
**Category:** Rails\
**Created:** [October 26, 2007, 11:14am UTC](https://www.ruby-forum.com/t/dynamic-finders-and-count/118383 "2007-10-26T11:14:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [October 26, 2007, 11:14am UTC](https://www.ruby-forum.com/t/dynamic-finders-and-count/118383/1 "2007-10-26T11:14:41Z")

</div>

Hi!

I have a (hopefully) simple problem, but just don’t get the idea how  
to implement it in rails.

There are two classes, one for users and one for comments. A user  
has\_many comments and each comment belongs\_to user.

I tried to do something like this:

@foo = User.find(:all, :order =\> ‘comments\_count’)

comments\_count is not known. An :include =\> ‘comments’ also doesn’t  
help (even that the SQL Syntax is doing a join now).

How can I find all Users sorted by the number of comments they have  
posted? It’s enough if the resulting list only contains the count-  
field as result. It is not necessary to have all comments “attached”  
to the list.

Thanks in advance!  
Markus

---

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [October 26, 2007, 11:43am UTC](https://www.ruby-forum.com/t/dynamic-finders-and-count/118383/2 "2007-10-26T11:43:21Z")

</div>

On 26 Oct 2007, at 10:13, lordbyte wrote:

> How can I find all Users sorted by the number of comments they have  
> posted? It’s enough if the resulting list only contains the count-  
> field as result. It is not necessary to have all comments “attached”  
> to the list.

Off the top of my head, something like this will do the job  
User.find :all, :select =\> ‘users._, count(_) as comment\_count’,  
:joins =\> ‘inner join comments on comments.id = comment\_id’,  
:group =\> ‘users.id’,  
:order =\> ‘comment\_count’

Fred

---

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [October 26, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/dynamic-finders-and-count/118383/3 "2007-10-26T13:13:38Z")

</div>

lordbyte wrote:

> I tried to do something like this:
> 
> @foo = User.find(:all, :order =\> ‘comments\_count’)
> 
> comments\_count is not known. An :include =\> ‘comments’ also doesn’t  
> help (even that the SQL Syntax is doing a join now).

There is no column named comments\_count therefore the problem you are  
getting. What you _could_ do is use a counter cache. Then your query  
would work as is.

Eric

---

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [October 26, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/dynamic-finders-and-count/118383/4 "2007-10-26T13:32:22Z")

</div>

Hi!

> There is no column named comments\_count therefore the problem you are  
> getting. What you _could_ do is use a counter cache. Then your query  
> would work as is.

That’s great - exactly what I searched! 🙂

Thanks for help!  
Markus
