Problem using hashes with 'find' method

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 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!

On 8/30/07, Toby R. [email protected] 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.

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:inexecute’
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:inselect_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:infind_every’
from
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active_record/base.rb:381:in
`find’
from (irb):3

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