-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
> Post.first.comments.sum(:helpfulness)
NoMethodError Exception: undefined method `+' for #<Comment:0x1083d5848>
> Post.first.comments.to_a.sum(&:helpfulness)
17
The first should work exactly like the second, except that the first should make one SUM query to the database, while the second loops over each comment and does numeric math.
In the end, they should both return the equiv of the following SQL that Rails generates:
SELECT SUM(usefulness) FROM comments WHERE post_id = 1;