开发者

Proper syntax for boolean condition in find

开发者 https://www.devze.com 2022-12-21 10:46 出处:网络
开发者_C百科I want to search through users whose show attribute is true. show = true @users = User.find(:all,

开发者_C百科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.


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.


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])


The syntax looks right to me.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号