我想在ActiveRecord中找到属性为nil
或某个值的记录:
class Model < ActiveRecord::Base
end
class CreateModels < ActiveRecord::Migration
def self.up
create_table :models do |t|
t.string :some_property
end
end
def self.down
drop_table :models
end
end
Model.all(:conditions => ["some_property IN (?)"], [nil, "this value"])
如何设置?
答案 0 :(得分:2)
试试这个
Model.all(:conditions =>
["some_property = ? or some_property is null", "this value"])
答案 1 :(得分:2)
您需要单独拆分NULL条件:
Model.all(:conditions => [ "some_property=? OR some_property IS NULL", "this value" ])
要测试值是否为NULL,您需要使用IS NULL
或IS NOT NULL
。