What you have wouldn't work.
@items = Item.where context: nil || ''
really evaluates to:
@items = Item.where context: ''
So you wouldn't find items with context
set to nil
using this method.
Side Note
Using thing || other
in this way never works. You cannot write if item == 'test' || 'something'
and expect it to work in cases where item
is 'something'
. It would only work in cases where item
is 'test'
. To get something like this to work you would need to write if item == 'test' || item == 'something'
. This isn't how humans talk, but it is how computers read.
Back to the main event
One way to write a query that would work is:
Item.where("context = ? or context = ?", nil, '')
This reads as: find all Items where context
is nil
or context
is ''
.
While this works, it's not considered particularly "Rails-y". A better way would be:
Item.where(context: [nil, ''])
This reads as: find all Items where context
is in [nil, '']
.
Related question