aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2014-09-08 13:12:04 +0930
committerMatthew Draper <matthew@trebex.net>2014-09-08 13:12:04 +0930
commitc3207a12be646483e7e0ce8c916e730e7ea5070d (patch)
treee79b4ac9fc7787e63612f6ee3c76a24da4b812c9 /activerecord/lib
parent42e69c352fc58f9af2c09dd7e8a7fa70bfe46898 (diff)
parent72d1663bc7353813f3ec5f4e841a2243baedb473 (diff)
downloadrails-c3207a12be646483e7e0ce8c916e730e7ea5070d.tar.gz
rails-c3207a12be646483e7e0ce8c916e730e7ea5070d.tar.bz2
rails-c3207a12be646483e7e0ce8c916e730e7ea5070d.zip
Merge pull request #16825 from cristianbica/fix-ar-nested-arrays
Fix query with nested array in Active Record
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder/array_handler.rb16
1 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb
index 78dba8be06..b8d9240bf8 100644
--- a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb
@@ -2,12 +2,20 @@ module ActiveRecord
class PredicateBuilder
class ArrayHandler # :nodoc:
def call(attribute, value)
- return attribute.in([]) if value.empty?
-
values = value.map { |x| x.is_a?(Base) ? x.id : x }
- ranges, values = values.partition { |v| v.is_a?(Range) }
nils, values = values.partition(&:nil?)
+ if values.any? { |val| val.is_a?(Array) }
+ ActiveSupport::Deprecation.warn "Passing a nested array to Active Record " \
+ "finder methods is deprecated and will be removed. Flatten your array " \
+ "before using it for 'IN' conditions."
+ values = values.flatten
+ end
+
+ return attribute.in([]) if values.empty? && nils.empty?
+
+ ranges, values = values.partition { |v| v.is_a?(Range) }
+
values_predicate =
case values.length
when 0 then NullPredicate
@@ -20,7 +28,7 @@ module ActiveRecord
end
array_predicates = ranges.map { |range| attribute.in(range) }
- array_predicates << values_predicate
+ array_predicates.unshift(values_predicate)
array_predicates.inject { |composite, predicate| composite.or(predicate) }
end