How do you say in SQL/ActiveRecord, "Find all records where attribute is nil or 'x'"?
I want to find records in ActiveRecord that have an attribute that is either nil
or some value:
class Model < ActiveRecord::Base
end
class Cre开发者_如何学PythonateModels < 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"])
How do I set that up?
Try this
Model.all(:conditions =>
["some_property = ? or some_property is null", "this value"])
You need to split out the NULL condition separately:
Model.all(:conditions => [ "some_property=? OR some_property IS NULL", "this value" ])
To test if a value is NULL, you need to use IS NULL
or IS NOT NULL
.
精彩评论