Karl Smith wrote:> My find_by_sql is not returning attributes of the calling model class.
>
> Thermo.find_by_sql("SELECT COUNT(*) FROM thermo WHERE thermo_loc >
''back'' GROUP BY DAYOFWEEK(time_on) ASC")
>
> # ==> [#<Thermo >, #<Thermo >, #<Thermo >, #<Thermo
>, #<Thermo >,
> #<Thermo >, #<Thermo >]
>
> The sql is fine and when I checked it in a mysql console, it returns
> the day of the week and count.
>
> Any ideas why Rails (2.1 btw) is not bringing in the resulting columns
> into Thermo model attributes?
>
> PS - if there is a way to rewrite this kind of query into AR, that
> would be great too.
>
> Karl
Hi Karl,
I''m just going to take a blind stab. Try including the group column in
the select statement and give the count an alias. Something like
select dayofweek(time_on) as day_of_week, count(*) as day_count
from thermo
where thermo_loc = ''back''
group by dayofweek(time_on)
As for how to write it in AR, you could probably do it with :select and
:group, but I haven''t tried.
Peace,
Phillip
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---