About ruby on rails, hackintosh, iphone3g and etc.

Thursday, February 26, 2009

SQL SUM() in Rails

In Rails, Active Record support many types of calculations. It saves us from executing SELECT SUM(x) using the query. Everything can be shorten down one line of code.

Example:
To find sum of all items in a location:

Item.sum('location_id', :conditions =>['location_id = ?', location.id])

Similar to avg and others. See ActiveRecord API for more info.

No comments: