Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper syntax for boolean condition in find

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.

like image 268
chief Avatar asked Aug 17 '26 10:08

chief


2 Answers

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.

like image 95
Peter Brown Avatar answered Aug 19 '26 09:08

Peter Brown


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])
like image 45
Harish Shetty Avatar answered Aug 19 '26 11:08

Harish Shetty