The correct syntax for a boolean condition in a search

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

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

This does not work for me.

+3
source share
4 answers

Your example should definitely work. If you are using an older version of the rails, you may need to restart the server.

Another option is to use hash syntax like ...

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

Then you can simply add your other conditions to the hash.

+5
source

Try an alternative version of the query:

User.find_all_by_show(true)

Make sure the table usershas a column tinyint(1)(ie boolean) with a name show.

. 1/0 true false . :

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

.

development.log? SQL .

+1
source

what database are you using? mysql or sqlite?

In mysql, boolean values ​​are stored as 1 and 0 and are different from another database, so in your migration you specified the default value: 1.

Please try again and try again.

As Toby Hede said, you can check the query generated in the log and try to run this query in the query browser or something else and see if you get the expected results.

0
source

Source: https://habr.com/ru/post/1734978/


All Articles