# Problem using hashes with 'find' method

**URL:** <https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355>\
**Category:** Rails\
**Created:** [August 30, 2007, 10:16am UTC](https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355 "2007-08-30T10:16:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [August 30, 2007, 10:16am UTC](https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355/1 "2007-08-30T10:16:08Z")

</div>

The following works for me:

Mymodel.find(:all, :conditions =\> “fieldOne = ‘Example1’ AND myobject\_id  
= ‘1’”)

but when I try

Mymodel.find(:all, :conditions =\> { :fieldOne =\> Example1, :myobject\_id  
=\> 1 })

… I get an error message

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
‘fieldOneExample1myobject\_id1’ in ‘where clause’: SELECT \* FROM mymodels  
WHERE (fieldOneExample1myobject\_id1)

The section ‘Conditions’ in  
[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) tells me that  
using a hash should generate an SQL statement based on equality and AND,  
but clearly something is not working here. I’m doing these tests using  
script/console but that shouldn’t make any difference should it?

thanks in advance for any help or suggestions!

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [August 30, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355/2 "2007-08-30T16:44:41Z")

</div>

On 8/30/07, Toby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> 1 })  
> That’s missing quotes around Example1. I suggest you cut and paste  
> your script/console session inputs and outputs exactly, so we can see  
> what’s happening.

Using a hash works fine for me, so something else is going on here.

> … I get an error message
> 
> ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
> ‘fieldOneExample1myobject\_id1’ in ‘where clause’: SELECT \* FROM mymodels  
> WHERE (fieldOneExample1myobject\_id1)

This looks like the hash is being converted to a string before it’s  
getting to AR, so we need to see _exactly_ what you’re doing.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [August 30, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355/3 "2007-08-30T18:54:51Z")

</div>

Here is a raw cut-and-paste from the console question:

> > Rtrevent.find(:all, :conditions =\> “event\_type = ‘Rteng’ AND router\_id = ‘1’”)  
> > =\> [#\<Rtrevent:0xb750d8b4 @attributes={“event\_id”=\>“1”, “id”=\>“5”,  
> > “router\_id”=\>“1”, “datetime”=\>“2007-05-12 18:34:51”,  
> > “event\_type”=\>“Rteng”}\>, #\<Rtrevent:0xb750d878  
> > @attributes={“event\_id”=\>“2”, “id”=\>“6”, “router\_id”=\>“1”,  
> > “datetime”=\>“2007-05-12 18:35:51”, “event\_type”=\>“Rteng”}\>,  
> > #\<Rtrevent:0xb750d83c @attributes={“event\_id”=\>“3”, “id”=\>“7”,  
> > “router\_id”=\>“1”, “datetime”=\>“2007-05-14 20:00:03”,  
> > “event\_type”=\>“Rteng”}\>]
> 
> > Rtrevent.find(:all, :conditions =\> { :event\_type =\> ‘Rteng’, :router\_id =\> ‘1’ } )  
> > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
> > ‘event\_typeRtengrouter\_id1’ in ‘where clause’: SELECT \* FROM rtrevents  
> > WHERE (event\_typeRtengrouter\_id1)  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
> > `log' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:185:in`execute’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/connection\_adapters/mysql\_adapter.rb:337:in  
> > `select' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:176:in`select\_all’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/base.rb:390:in  
> > `find_by_sql' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:924:in`find\_every’  
> > from  
> > ./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/base.rb:381:in  
> > `find’  
> > from (irb):3

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [August 30, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/problem-using-hashes-with-find-method/113355/4 "2007-08-30T20:32:40Z")

</div>

I would verify that you are running the latest stable version of Ruby  
on Rails. The hash-based condition clauses were a semi-recent addition  
to the framework, and judging by the flattening of the hash into a  
string, my first guess is that that is the case.

Taylor S.,  
Reality Technician  
[http://realitytechnicians.com](http://realitytechnicians.com)
