# Find\_by\_sql Doesn't Return Attributes?

**URL:** <https://discuss.rubyonrails.org/t/find-by-sql-doesnt-return-attributes/30991>\
**Category:** rubyonrails-talk\
**Created:** [June 24, 2008, 1:03am UTC](https://discuss.rubyonrails.org/t/find-by-sql-doesnt-return-attributes/30991 "2008-06-24T01:03:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 24, 2008, 1:03am UTC](https://discuss.rubyonrails.org/t/find-by-sql-doesnt-return-attributes/30991/1 "2008-06-24T01:03:35Z")

</div>

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
