#14 Performing Calculations on Models

Did you know ActiveRecord provides class methods for performing calculations on models? You can even use these methods through associations.

Task.sum(:priority)
Task.sum(:priority, :conditions => 'complete=0')
Task.maximum(:priority)
Task.minimum(:priority)
Task.average(:priority)
p = Project.find(:first)
p.tasks.sum(:priority)
p.tasks.sum(:priority, :conditions => 'complete=0')

你可能感兴趣的:(Railscasts)