第一个参数为 nil 时的 ActiveRecord OR 查询行为
ActiveRecord OR query behavior when first argument is nil
有 foo=nil
和 bar=43
的记录。为什么以下查询与该记录不匹配。它不应该只检查 foo
因为它与给定值匹配(即 nil
)吗?
myrecords.where("foo = ? OR bar = ?", nil, 42).first
嘿,你可以尝试这种方式,空值不在 mysql 中进行比较,有关 NULL
值的更多信息,请参阅
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
这应该有效:
myrecords.where("foo = ? OR bar = ?", NULL, 42).first
有 foo=nil
和 bar=43
的记录。为什么以下查询与该记录不匹配。它不应该只检查 foo
因为它与给定值匹配(即 nil
)吗?
myrecords.where("foo = ? OR bar = ?", nil, 42).first
嘿,你可以尝试这种方式,空值不在 mysql 中进行比较,有关 NULL
值的更多信息,请参阅
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
ornot null
used NULL-safe equal to operator of mysql as:
myrecords.where("foo <=> ? OR bar = ?", nil, 42).first
这应该有效:
myrecords.where("foo = ? OR bar = ?", NULL, 42).first