balki
balki

Reputation: 27664

ActiveRecord OR query behavior when first argument is nil

There is a record with foo=nil and bar=43. Why does the following query does not match that record. Should it not just check only foo since it matches with the given value (i.e nil)?

myrecords.where("foo = ? OR bar = ?", nil, 42).first

Upvotes: 1

Views: 82

Answers (2)

Vishal Jain
Vishal Jain

Reputation: 1940

Hey you can try this way null value is not compare in mysql for more info of NULL values refer Working with NULL Values

myrecords.where("foo is null OR bar = ?", 42).first

If in case you are not aware of value of object is null or not null used NULL-safe equal to operator of mysql as:

 myrecords.where("foo <=> ? OR bar = ?", nil, 42).first

Upvotes: 2

bo-oz
bo-oz

Reputation: 2872

This should work:

myrecords.where("foo = ? OR bar = ?", NULL, 42).first

Upvotes: -1

Related Questions