# Record count

**URL:** <https://www.ruby-forum.com/t/record-count/80207>\
**Category:** Rails\
**Created:** [December 22, 2006, 6:19am UTC](https://www.ruby-forum.com/t/record-count/80207 "2006-12-22T06:19:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3d9bf3/32.png) [@frank](https://www.ruby-forum.com/u/frank)\
**Post date:** [December 22, 2006, 6:19am UTC](https://www.ruby-forum.com/t/record-count/80207/1 "2006-12-22T06:19:10Z")

</div>

I have a query that selects all records from a db. In the view i loop  
through the records.

Does rails setup a variable that holds the amount of records the query  
returned? Similar to Coldfusions recordcount?

---

<div class="post-metadata">

**Author:** ![frank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3d9bf3/32.png) [@frank](https://www.ruby-forum.com/u/frank)\
**Post date:** [December 22, 2006, 7:18am UTC](https://www.ruby-forum.com/t/record-count/80207/2 "2006-12-22T07:18:45Z")

</div>

hi,

from doc

Examples for counting all:

Person.count # returns the total count of all people

Examples for count by conditions and joins (this has been deprecated):

Person.count(“age \> 26”) # returns the number of people older than 26  
Person.find(“age \> 26 AND job.salary \> 60000”, “LEFT JOIN jobs on  
jobs.person\_id = person.id”) # returns the total number of rows  
matching the conditions and joins fetched by SELECT COUNT(\*).

Examples for count with options:

Person.count(:conditions =\> “age \> 26”)  
Person.count(:conditions =\> “age \> 26 AND job.salary \> 60000”,  
:include =\> :job) # because of the named association, it finds the  
DISTINCT count using LEFT OUTER JOIN.  
Person.count(:conditions =\> “age \> 26 AND job.salary \> 60000”,  
:joins =\> “LEFT JOIN jobs on jobs.person\_id = person.id”) # finds the  
number of rows matching the conditions and joins.  
Person.count(‘id’, :conditions =\> “age \> 26”) # Performs a COUNT(id)  
Person.count(:all, :conditions =\> “age \> 26”) # Performs a COUNT(_)  
(:all is an alias for '_’)

Note: Person.count(:all) will not work because it will use :all as the  
condition. Use Person.count instead.

might help

regards

gaurav

---

<div class="post-metadata">

**Author:** ![frank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3d9bf3/32.png) [@frank](https://www.ruby-forum.com/u/frank)\
**Post date:** [December 22, 2006, 8:04am UTC](https://www.ruby-forum.com/t/record-count/80207/3 "2006-12-22T08:04:48Z")

</div>

On Dec 21, 11:16 pm, “gaurav bagga” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Examples for counting all:
> 
> Person.count # returns the total count of all people

If you only want the counts then the above method is considerably  
faster. If you want the records AND the count then just use “length”.  
ActiveRecord finds return an array, and array.length will tell you how  
many records were returned.

people = Person.find(:all)  
puts “#{people.length} records returned”  
people.each {|person| puts person.name}

Aaron

---

<div class="post-metadata">

**Author:** ![frank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3d9bf3/32.png) [@frank](https://www.ruby-forum.com/u/frank)\
**Post date:** [December 22, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/record-count/80207/4 "2006-12-22T17:43:25Z")

</div>

Aaron wrote:

> On Dec 21, 11:16 pm, “gaurav bagga” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Examples for counting all:
> > 
> > Person.count # returns the total count of all people
> 
> If you only want the counts then the above method is considerably  
> faster. If you want the records AND the count then just use “length”.  
> ActiveRecord finds return an array, and array.length will tell you how  
> many records were returned.
> 
> people = Person.find(:all)  
> puts “#{people.length} records returned”  
> people.each {|person| puts person.name}
> 
> Aaron

Length is what I was looking for, thank you.
