method
average
v3.0.0 -
Show latest stable
- Class:
ActiveRecord::Calculations
average(column_name, options = {})public
Calculates the average value on a given column. Returns nil if there’s no row. See calculate for examples with options.
Person.average('age') # => 35.8
1Note
No type cast to Float
In Rails 3 the returned value will be type cast to the column's type and not Float. So when calculating average on a column the column's type need to be float, the result will be truncated otherwise.