# Count of records meeting condition

**URL:** <https://www.ruby-forum.com/t/count-of-records-meeting-condition/193959>\
**Category:** Rails\
**Created:** [August 27, 2010, 7:13am UTC](https://www.ruby-forum.com/t/count-of-records-meeting-condition/193959 "2010-08-27T07:13:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 27, 2010, 7:13am UTC](https://www.ruby-forum.com/t/count-of-records-meeting-condition/193959/1 "2010-08-27T07:13:19Z")

</div>

Forgive this question, but I’m new to rails, and my SQL is rather rusty.  
I have a line like so to get all records for a particular time period:

@recordings\_for\_period = Recording.find(:all, :order =\> “date\_of\_event  
DESC”, :conditions =\> [“date\_of\_event \>= ? AND date\_of\_event \< ?”,  
start\_date, end\_date])

Let say, instead, I just want to know the number of recordings that are  
available for that time period. Should I just run the above code and  
then get the length of the array? Or is there some construction I should  
be using with COUNT that would be better? (I.e., is less work for the  
database to process.)

Oh, and (unfortunately) I am still using rails 2.3.5.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 27, 2010, 10:04am UTC](https://www.ruby-forum.com/t/count-of-records-meeting-condition/193959/2 "2010-08-27T10:04:33Z")

</div>

On 27 August 2010 06:13, Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> be using with COUNT that would be better? (I.e., is less work for the  
> database to process.)

@count\_of\_recordings\_for\_period = Recording.count(:all, :order =\>  
“date\_of\_event  
DESC”, :conditions =\> [“date\_of\_event \>= ? AND date\_of\_event \< ?”,  
start\_date, end\_date])

should use COUNT in the sql to give the answer you want.

Colin

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 27, 2010, 9:59am UTC](https://www.ruby-forum.com/t/count-of-records-meeting-condition/193959/3 "2010-08-27T09:59:17Z")

</div>

On 27 Aug, 06:13, Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> be using with COUNT that would be better? (I.e., is less work for the  
> database to process.)

There’s a count method that does what you want

Fred
