method
count
v4.1.8 -
Show latest stable
-
0 notes -
Class: ActiveRecord::Calculations
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-38)
- 4.1.8 (5)
- 4.2.1 (6)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (2)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (6)
- 7.1.3.4 (0)
- What's this?
count(column_name = nil, options = {})
public
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 }
If count is used with select, it will count the selected columns:
Person.select(:age).count # => counts the number of different age values
Note: not all valid select expressions are valid count expressions. The specifics differ between databases. In invalid cases, an error from the databsae is thrown.