Ruby on Rails 2.1 - The Maths Functions


Consider a table object called Person. This table is having fields like age, first_name, last_name and salary.

Following will return average age of all the employee.

Person.average :age

Following will return maximum age of the employee.

Person.maximum :age

Following will return minimum age of the employee.

Person.minimum :age

Follwoing will return sum of salaries of all the employees.

Person.sum :salary, :group => :last_name

Following will count the number of records having age more than 26.

Person.count(:conditions => "age > 26")

Following count total number of records.