method

average

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

lawitschka ยท Feb 14, 2011

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.