Record count

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?

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

On Dec 21, 11:16 pm, “gaurav bagga” [email protected] 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

Aaron wrote:

On Dec 21, 11:16 pm, “gaurav bagga” [email protected] 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.