How do you say在SQL / ActiveRecord,“查找所有记录属性为零或'x'”?

时间:2010-09-21 13:37:34

标签: sql ruby-on-rails activerecord

我想在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"])

如何设置?

2 个答案:

答案 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 NULLIS NOT NULL