I have two tables: one called Projects, and another called Documents (projects has many documents, documents belong to projects).
The Projects table has a “limit” integer column wherein the user can specify the number of Documents they need.
I want to add up all of the limits and get a single value. So for example, if project A has a limit of 5, project B has a limit of 10, and project C has a limit of 8, I want to be able to display “23”.
What’s the proper Ruby command for this?
ActiveRecord::Baseexposes the SQLSUMfunction as the sum method, soshould do the trick. This is aware of scopes and so on, so you could do
to calculate the sum only for rows where
activewas true orto sum the limits of the projects associated with a person (assuming person
has_manyor habtm projects)MIN,MAXandAVERAGEare exposed in similar ways.