views:

23

answers:

2

I want to find records in ActiveRecord that have an attribute that is either nil or some value:

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"])

How do I set that up?

+2  A: 

Try this

Model.all(:conditions => 
  ["some_property = ? or some_property is null", "this value"])
Steve Weet
+1  A: 

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.

tadman