Skip to content

Commit

Permalink
Merge pull request rails#16825 from cristianbica/fix-ar-nested-arrays
Browse files Browse the repository at this point in the history
Fix query with nested array in Active Record
  • Loading branch information
matthewd committed Sep 8, 2014
2 parents 42e69c3 + 72d1663 commit c3207a1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +28,7 @@ def call(attribute, value)
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

Expand Down
28 changes: 28 additions & 0 deletions activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,34 @@ def test_find_on_hash_conditions_with_array_of_integers_and_ranges
assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [1..2, 3, 5, 6..8, 9]).to_a.map(&:id).sort
end

def test_find_on_hash_conditions_with_array_of_ranges
assert_equal [1,2,6,7,8], Comment.where(id: [1..2, 6..8]).to_a.map(&:id).sort
end

def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges
assert_deprecated do
assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [[1..2], 3, [5], 6..8, 9]).to_a.map(&:id).sort
end
end

def test_find_on_hash_conditions_with_array_of_integers_and_arrays
assert_deprecated do
assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [[1, 2], 3, 5, [6, [7], 8], 9]).to_a.map(&:id).sort
end
end

def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges_and_nils
assert_deprecated do
assert_equal [1,3,4,5], Topic.where(parent_id: [[2..6], nil]).to_a.map(&:id).sort
end
end

def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges_and_more_nils
assert_deprecated do
assert_equal [], Topic.where(parent_id: [[7..10, nil, [nil]], [nil]]).to_a.map(&:id).sort
end
end

def test_find_on_multiple_hash_conditions
assert Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: false).find(1)
assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/cases/relation/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def test_where_with_table_name_and_empty_array
end

def test_where_with_table_name_and_nested_empty_array
assert_equal [], Post.where(:id => [[]]).to_a
assert_deprecated do
assert_equal [], Post.where(:id => [[]]).to_a
end
end

def test_where_with_empty_hash_and_no_foreign_key
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/fixtures/developers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ dev_<%= digit %>:
poor_jamis:
id: 11
name: Jamis
salary: 9000
salary: 9000

0 comments on commit c3207a1

Please sign in to comment.