supporting nil when passed in as an IN clause

This commit is contained in:
Aaron Patterson 2011-04-29 11:01:59 -07:00
parent 66a18855ea
commit 850b3ca477
2 changed files with 16 additions and 4 deletions

View File

@ -25,7 +25,18 @@ module ActiveRecord
values = value.to_a.map { |x|
x.is_a?(ActiveRecord::Base) ? x.id : x
}
attribute.in(values)
if values.include?(nil)
values = values.compact
if values.empty?
attribute.eq nil
else
attribute.in(values.compact).or attribute.eq(nil)
end
else
attribute.in(values)
end
when Range, Arel::Relation
attribute.in(value)
when ActiveRecord::Base

View File

@ -1045,7 +1045,7 @@ class FinderTest < ActiveRecord::TestCase
:order => ' author_addresses_authors.id DESC ', :limit => 3).size
end
def test_find_with_nil_inside_set_passed_for_attribute
def test_find_with_nil_inside_set_passed_for_one_attribute
client_of = Company.find(
:all,
:conditions => {
@ -1054,7 +1054,8 @@ class FinderTest < ActiveRecord::TestCase
:order => 'client_of DESC'
).map { |x| x.client_of }
assert_equal [2, 1, nil], client_of
assert client_of.include?(nil)
assert_equal [2, 1].sort, client_of.compact.sort
end
def test_find_with_nil_inside_set_passed_for_attribute
@ -1064,7 +1065,7 @@ class FinderTest < ActiveRecord::TestCase
:order => 'client_of DESC'
).map { |x| x.client_of }
assert_equal [nil], client_of
assert_equal [], client_of.compact
end
def test_with_limiting_with_custom_select