# Add a column count(id) to the select statement, rail way

**URL:** <https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558>\
**Category:** Rails\
**Created:** [September 13, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558 "2006-09-13T16:25:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor\_MilovanoviSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7c8e57/32.png) [@Igor\_MilovanoviSSS](https://www.ruby-forum.com/u/Igor_MilovanoviSSS)\
**Post date:** [September 13, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558/1 "2006-09-13T16:25:29Z")

</div>

I would like to do something like this:  
SELECT \*,count(ID) FROM downloads GROUP BY file\_path ORDER BY user\_id  
LIMIT 10;

I know it can be done with find\_by\_sql, but don’t know if that can be  
done by just find.

Or is there better Rails way to do this?

Thanks.

–  
Igor MILOVANOVIÆ  
[http://f13o.pletisan.rs.ba/blog/](http://f13o.pletisan.rs.ba/blog/)

---

<div class="post-metadata">

**Author:** ![Igor\_MilovanoviSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7c8e57/32.png) [@Igor\_MilovanoviSSS](https://www.ruby-forum.com/u/Igor_MilovanoviSSS)\
**Post date:** [September 13, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558/2 "2006-09-13T17:31:42Z")

</div>

I think you risk running 2 queries if you do it the rails way of  
Model.count().  
Up to you really. Its probably better to do a custom find\_by\_sql to  
limit  
the queries you run against your DB.

Adam

---

<div class="post-metadata">

**Author:** ![Igor\_MilovanoviSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7c8e57/32.png) [@Igor\_MilovanoviSSS](https://www.ruby-forum.com/u/Igor_MilovanoviSSS)\
**Post date:** [September 13, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558/3 "2006-09-13T22:44:27Z")

</div>

> I would like to do something like this:  
> SELECT \*,count(ID) FROM downloads GROUP BY file\_path ORDER BY  
> user\_id LIMIT 10;
> 
> I know it can be done with find\_by\_sql, but don’t know if  
> that can be done by just find.
> 
> Or is there better Rails way to do this?

If by “Rails way” you mean something built in to ActiveRecord, then  
you’re talking about counter\_cache.

class Download \< ActiveRecord::Base  
belongs\_to :product, :counter\_cache =\> true  
end

Then in your product table, you would have a downloads\_count column  
(int, default of 0).

When you do that you’ll be able to access  
product.downloads.size

_WARNING_ I recall there being a prior thread talking about a bug in  
save() that blows away the count, or something like that. So I don’t  
know if this solution is broken or not at this point.

- Mark.

---

<div class="post-metadata">

**Author:** ![Igor\_MilovanoviSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7c8e57/32.png) [@Igor\_MilovanoviSSS](https://www.ruby-forum.com/u/Igor_MilovanoviSSS)\
**Post date:** [September 14, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/add-a-column-count-id-to-the-select-statement-rail-way/68558/4 "2006-09-14T12:28:34Z")

</div>

Thanks for replies, I will have a look.  
But this find\_by\_sql solution is working like a charm for me…  
So, I might just leave it that way…

On 9/13/06, Thomas, Mark - BLS CTR [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

–  
Igor MILOVANOVIÆ  
[http://f13o.pletisan.rs.ba/blog/](http://f13o.pletisan.rs.ba/blog/)
