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

Read more at the source