On 6 March 2012 15:08, John Merlino <[email protected]> wrote:
> Hey all,
>
> So I have a custom query that I embedded into a class method
> definition:
>
>
> def self.get_sum_for_range(unit_id,report1,report2)
>     find_by_sql(["SELECT SUM(distance * 0.000621371192) as sum FROM
> reports WHERE unit_id=? AND id >= ? AND id <= ?", unit_id, report1,
> report2])
> end
>
> value  = Report.get_sum_for_range(unit.id,report1,report2)
>
>
> The problem is value will be a report object that contains an
> attribute called sum with the value, but all I want is just the value
> itself returned into value.

def self.get_sum_for_range(unit_id,report1,report2)
    find_by_sql(["SELECT SUM(distance * 0.000621371192) as sum FROM
reports WHERE unit_id=? AND id >= ? AND id <= ?", unit_id, report1,
report2]).sum
end

Note the .sum on the end of find_by_sql

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to