I posted a similar question to this not too long ago in regards to formatting a MySQL query using a block and got very good responses, but they were very specific to the problem at hand. This time around, I'm dealing with getting the .sum()
s of rows in a table. Here's what I've got now:
def balance
balance = 0
items.each do |item|
balance = balance + item.charges.sum(:revenue, :conditions => ['created_at >= ?', Time.now.beginning_of_month])
end
balance
end
My goal here is to get the total of all charges for this month for a given user. Charges belong to items, which belong users. I'm sure there's a better way to do this in Ruby/Rails.
What would you do?