Search doesn’t seem to be working, so forgive me if I’m covering old
ground here, but I’m having a difficult time figuring out how to
implement a “SUM()” function using Active record. Obviously, I could
just create a query, but I’d rather make it part of my active record
object.
Basically I have an object called “Report < ActiveRecord::Base” with a
field called “hours” (work hours). Here’s how I access it from within my
helper function:
Report.find(:first, :conditions => “date = '” <<
current_date.strftime("%Y-%m-%d") << “’ AND user_id = '” <<
@user.id.to_s << “’”)
However, in some cases I want the exact same info as above, except a
running total of the entire week. Here’s more or less how I’d like to
access it (though this returns the first field in a record set, rather
than a single, summed record):
Report.find(:first, :conditions => “date >= '” << begin_date << “’ AND
date <= '” << end_date << “’ AND user_id = '” << @user.id.to_s << “’”)
As I said, the above just returns the first field of a recordset for the
given date range – not a sum of the entire week. I could iterate
through that record set and create a running total, but that doesn’t
seem like the right way to do it.