views:

58

answers:

4

I want to search through users whose show attribute is true.

show = true
@users = User.find(:all,
               :conditions => ["show = ?", show])

This doesn't appear to be working for me.

+1  A: 

The syntax looks right to me.

What does your development.log reveal? The query SQL should appear there as executed.

Toby Hede
This is the error: ActiveRecord::StatementInvalid (Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show = 1)
chief
A: 

Try an alternative version of the query:

User.find_all_by_show(true)

Make sure the users table has a tinyint(1)(i.e. boolean) column called show.

I have seen this behavior before. I had to use 1/0 for true false for array conditions. Try this:

show = 1
@users = User.find(:all,
               :conditions => ["show = ?", show])
KandadaBoggu
Yes this works, using the console.
chief
I prefer to use the other way though, as I have other conditions in the find.
chief
I have updated my answer. Try the second method I proposed in my answer.
KandadaBoggu
+1  A: 

Your example should definitely work. If you're running an older version of rails you may need to restart your server.

Another option is to use the hash syntax like...

@users = User.find(:all, :conditions => {:show => true})

Then you can just add your other conditions within the hash.

Beerlington
This hash will work for me.
chief
A: 

which database are you using? mysql or sqlite?

In mysql boolean values are stored as 1 and 0 and it differs with other database, so in your migration you specified default value is 1.

Please check it again and try again.

As Toby Hede said you can check the query generated in the log and try to run that query in your query browser or anything and see whether you are getting the expected results.

vamsi