Forum: Ruby on Rails Using hashes as find conditions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Saxon (Guest)
on 2007-04-12 21:32
(Received via mailing list)
I've been having problems using hashes in conjunction with SQL. Here's
an example:

   Question.count(:all, :conditions => { :parent => nil })

And here's the query it runs :

   SELECT count(*) AS count_all FROM questions WHERE (parent)

If I try to do something like:

   Question.count(:all, :conditions => { :parent => 1 })

I get:

   Mysql::Error: #42S22Unknown column 'parent1' in 'where clause':
SELECT count(*) AS count_all FROM questions WHERE (parent1)

Why does it strip out the conditional text?

I'm running Rails 1.2.3

Thanks in advance for any help!

Saxon
Chris H. (Guest)
on 2007-04-12 21:47
(Received via mailing list)
using hashed conditions only works in 1.2.x i believe.

1.1.6 behavior

{ :parent => nil } # => select * from ... where (parent)
{ :parent => 1 } # => select * from ... where (parent1)

1.2.3 behavior

{ :parent => nil } # => select * from ... where (parent IS NULL)
{ :parent => 1 } # => select * from ... where (parent = 1)
Chris H. (Guest)
on 2007-04-12 21:49
(Received via mailing list)
sorry, missed that you said you were using 1.2.3.

did you create your project using a previous version of rails?  if so,
check your config/environment.rb file.  that will specify which
version your project is using.
Saxon (Guest)
on 2007-04-12 22:14
(Received via mailing list)
Well don't I feel silly. I did in fact create the application using
1.1.6, so I updated the number in the environment.rb file and it works
just fine. Thank you for your help!

Saxon
This topic is locked and can not be replied to.