I have a survey model that works like so:
ResponseSets have many Responses
Responses belong_to Answer
Answer model has a “value” column.
Given a ResponseSet, I’d like the sum of the Answers that are associated with each Response.
Ie, what I’d like to be able to do, (in imaginary code) is:
response_set.responses.answers.sum('value')
However, this obviously doesn’t work, I need to build a query through response_set.responses, but I don’t know how.
What’s the SQL-fu way to tackle this in ActiveRecord?
After much trial and error I came up with this relatively simple solution, I hope this helps others in the future:
To make this more convenient I just made this a method in the
ResponseSetModel: