You can perform a direct conversion:
def balance
conds = ["created_at > ?", Time.now.beginning_of_month]
items.inject(0) do |total, item|
total + item.charges.sum(:revenue, :conditions => conds)
end
end
, , . , - :
def balance
Charge.sum :revenue,
:conditions => ["charges.item_id IN (?) AND created_at > ?",
items.map { |item| item.id },
Time.now.beginning_of_month]
end
map, inject, select .. . RDoc Enumerable .