Method: ActiveRecord::Calculations#count

Defined in:
activerecord/lib/active_record/relation/calculations.rb

#count(column_name = nil, options = {}) ⇒ Object

Count the records.

Person.count
# => the total count of all people

Person.count(:age)
# => returns the total count of all people whose age is present in database

Person.count(:all)
# => performs a COUNT(*) (:all is an alias for '*')

Person.distinct.count(:age)
# => counts the number of different age values

If count is used with group, it returns a Hash whose keys represent the aggregated column, and the values are the respective amounts:

Person.group(:city).count
# => { 'Rome' => 5, 'Paris' => 3 }


22
23
24
25
# File 'activerecord/lib/active_record/relation/calculations.rb', line 22

def count(column_name = nil, options = {})
  column_name, options = nil, column_name if column_name.is_a?(Hash)
  calculate(:count, column_name, options)
end