Ruby on Rails 2.1 - The Maths Functions


Advertisements


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

The following code will return the average age of all the employees.

Person.average :age

The following code will return the maximum age of the employees.

Person.maximum :age

The following code will return the minimum age of the employees.

Person.minimum :age

The following code will return the sum of salaries of all the employees.

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

The following code will count the number of records having age more than 26.

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

The following code will count the total number of records.

Person.count

rails-quick-guide.htm

Advertisements