# Automatic type casting of query parameters in finds

**URL:** <https://www.ruby-forum.com/t/automatic-type-casting-of-query-parameters-in-finds/194982>\
**Category:** Rails\
**Created:** [September 14, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/automatic-type-casting-of-query-parameters-in-finds/194982 "2010-09-14T19:11:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [September 14, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/automatic-type-casting-of-query-parameters-in-finds/194982/1 "2010-09-14T19:11:46Z")

</div>

I’m new to ruby on rails and am using active record 3 to create some  
object models on top of a postgres db.

I have an Environment record with a :name attribute that maps to the  
:name column of type :string. When I do Environment.find\_by\_name( “abc”  
), everything works as expected. However, when I do  
Environment.find\_by\_name( 123 ), my code breaks with the following  
error:

ERROR: operator does not exist: character varying = integer  
(ActiveRecord::StatementInvalid)  
LINE 1: …nts".“purpose” = ‘test’ AND “environments”.“name” = 123)  
LI…  
^  
HINT: No operator matches the given name and argument type(s). You  
might need to add explicit type casts.

So my question is, if activerecord knows the type of the :name column  
(:string), then why doesn’t it automatically call to\_s on the :name  
parameter to avoid this issue?

I have a work around right now that overrides find, and explicitly does  
this type casting, but I’m guessing there is a cleaner way to achieve  
this behavior.

Thanks,  
Alex
