Find_by_sql Doesn't Return Attributes?

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”)

==> [#, #, #, #, #,

#, #]

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

Karl S. 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”)

==> [#, #, #, #, #,

#, #]

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