record count

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](http://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](http://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

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