aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-04-29 11:01:59 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-04-29 11:01:59 -0700
commit850b3ca4779da201d4805a12fe29d9d9a491739e (patch)
tree438cede35a48ba1be74f4322d8df01a22416592f /activerecord
parent66a18855eafa71c11a37333ce1314889cbd0f742 (diff)
downloadrails-850b3ca4779da201d4805a12fe29d9d9a491739e.tar.gz
rails-850b3ca4779da201d4805a12fe29d9d9a491739e.tar.bz2
rails-850b3ca4779da201d4805a12fe29d9d9a491739e.zip
supporting nil when passed in as an IN clause
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb13
-rw-r--r--activerecord/test/cases/finder_test.rb7
2 files changed, 16 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index 982b3d7e9f..2814771002 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -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
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 9b6363902e..be4ba18555 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -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