aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorCristian Bica <cristian.bica@gmail.com>2014-09-06 10:25:36 +0300
committerCristian Bica <cristian.bica@gmail.com>2014-09-06 23:46:32 +0300
commit72d1663bc7353813f3ec5f4e841a2243baedb473 (patch)
tree8fdb76e18556fe6c1344170377be2ad529352296 /activerecord/lib
parentda2f61947db287b5ba0343905da9316eecfd43f3 (diff)
downloadrails-72d1663bc7353813f3ec5f4e841a2243baedb473.tar.gz
rails-72d1663bc7353813f3ec5f4e841a2243baedb473.tar.bz2
rails-72d1663bc7353813f3ec5f4e841a2243baedb473.zip
Fix query with nested array in Active Record
`User.where(id: [[1,2],3])` was equal to `User.where(id:[1, 2, 3])` in Rails 4.1.x but because of some refactoring in Arel this stopped working in 4.2.0. This fixes it in Rails. [Dan Olson & Cristian Bica]
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