When doing a search in active record I'm looking for record's that do not have an archived bit set to true.
Some of the archived bits are null (which are not archived) others have archived set to false.
Obviously,
Project.all(:conditions => {:archived => false})
misses the projects with the archived bits with null values. How can all non-archived projects be selected wtih active record?
Rails 4 (possibly earlier) supports:
Project.where(archived: [false, nil])
... which is quite concise.
Try this (in Rails 2):
Project.all(:conditions => ['archived IS NULL OR archived = ?', false])
This is a limitation of older versions of Rails, as explained here: https://rails.lighthouseapp.com/projects/8994/tickets/1181-ar-find-producing-null-when-it-should-be-is-null
The proper database agnostic way to do this is:
Project.where("archived IS NULL OR archived = ?", false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With