I have a Post model, with a posted boolean field.
I created a new Post, without assigning anything to posted.
irb(main):012:0> Post.first.posted
Post Load (9.0ms) SELECT "posts".* FROM "posts" LIMIT 1
=> nil
Now, I want to select all posts where posted is not equal to true.
I tried this, but it doesn’t work:
irb(main):016:0> Post.where("posted != ?", true)
Post Load (2.5ms) SELECT "posts".* FROM "posts" WHERE (posted != 't')
=> []
Any ideas?
It is because of how the database treats NULL. Try setting
:default => falsein your migration.Depending on your database, you might see something like this: (example in MySQL)