On 6 March 2012 16:12, Michael Pavling <[email protected]> wrote:
> On 6 March 2012 15:46, Colin Law <[email protected]> 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)
I was thinking more along the lines of removing find_by_sql, something like
Report.where("unit_id=? AND id >= ? AND id <= ?", unit_id, report1,
report2).sum('distance')
>
> ...but I think this is straying from the topic :-)
True, but may still be helping the OP.
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.
>
--
gplus.to/clanlaw
--
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.