Re: [Rails] getting find_by_sql to return the numeric value rather than an object that contains the value as an attribute
On 6 March 2012 15:46, Colin Law <clanlaw@googlemail.com> wrote:
>> Ah, yeah. Do it in the method.
>
> Is there not a more railsy way of doing what the op wants rather than
> using find_by_sql? At least it would be more efficient I think to do
> the * 0.006... after the find.
>
> find_by_sql(["SELECT SUM(distance) as sum FROM
> reports WHERE unit_id=? AND id >= ? AND id <= ?", unit_id, report1,
> report2]).sum * 0.000621371192
Hmmm... yes, I wonder whether the method is doing too much. That's a
conversion to meters from miles, but there's a "unit_id" being passed
(and I wonder what the intention of that is)... so it might be better
to have a couple of more reusable methods:
def self.get_sum_for_range(unit_id,report1,report2)
find_by_sql(["SELECT SUM(distance) as sum FROM reports WHERE
unit_id=? AND id >= ? AND id <= ?", unit_id, report1, report2]).sum
end
def self.get_sum_of miles_in_meters(report1,report2)
Report.get_sum_for_range(unit.id,report1,report2) * 0.000621371192
end
value = Report.get_sum_of miles_in_meters(report1, report2)
...but I think this is straying from the topic :-)
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home