# ActiveRecord returning Strings with find\_by\_sql?

**URL:** <https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559>\
**Category:** Rails\
**Created:** [March 8, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559 "2006-03-08T16:32:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Mike\_Marcus](https://www.ruby-forum.com/u/Mike_Marcus)\
**Post date:** [March 8, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559/1 "2006-03-08T16:32:25Z")

</div>

Hello,

I’m new to Ruby & Rails, and I’m trying to figure out if this  
behavior by ActiveRecord is by design, or if I’m doing something  
incorrectly. Here’s the deal: I have a class based on  
ActiveRecord::Base that defines a method that creates a query using  
find\_by\_sql(). The fields retrieved by joins with other tables are  
being returned as Strings, regardless of field type. Fields from the  
table that the class directly corresponds to are being returned as  
whatever type is appropriate. Is that how it’s supposed to work? I  
find myself having to do a lot of conversions when I need to perform  
any math on the results obtained from this query.

Thanks for your help,

Mike Marcus  
Software Engineer  
[Broadband.com](http://Broadband.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Mike\_Marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Mike\_Marcus](https://www.ruby-forum.com/u/Mike_Marcus)\
**Post date:** [March 8, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559/2 "2006-03-08T23:56:49Z")

</div>

Mike Marcus wrote:

> to do a lot of conversions when I need to perform any math on the  
> results obtained from this query.
> 
> Thanks for your help,

It’s a long known deficiency of the current ActiveRecord implementation  
([http://dev.rubyonrails.org/ticket/240](http://dev.rubyonrails.org/ticket/240)). Likely not to change very soon.

– stefan

–  
For rails performance tuning, see: [http://railsexpress.de/blog](http://railsexpress.de/blog)  
Subscription: [railsexpress.de](http://railsexpress.de/blog/xml/rss20/feed.xml)

---

<div class="post-metadata">

**Author:** ![Mike\_Marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Mike\_Marcus](https://www.ruby-forum.com/u/Mike_Marcus)\
**Post date:** [March 8, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559/3 "2006-03-08T23:56:51Z")

</div>

Mike Marcus wrote:

> I’m new to Ruby & Rails, and I’m trying to figure out if this behavior  
> by ActiveRecord is by design, or if I’m doing something incorrectly.  
> Here’s the deal: I have a class based on ActiveRecord::Base that  
> defines a method that creates a query using find\_by\_sql(). The fields  
> retrieved by joins with other tables are being returned as Strings,  
> regardless of field type. Fields from the table that the class directly  
> corresponds to are being returned as whatever type is appropriate. Is  
> that how it’s supposed to work? I find myself having to do a lot of  
> conversions when I need to perform any math on the results obtained  
> from this query.

That’s how it works. You only get automatic casting for fields in  
the model on which find is called, and only for fields from joined  
tables when you use find’s :include option.

You can however define custom reader and writer methods for the  
joined fields so that you only have to write the casting code once.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Mike\_Marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Mike\_Marcus](https://www.ruby-forum.com/u/Mike_Marcus)\
**Post date:** [March 9, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/activerecord-returning-strings-with-find-by-sql/54559/4 "2006-03-09T19:49:23Z")

</div>

Thanks, Mark and Stefan for your replies!

- Mike
