mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fixes issue #3483, regarding using a mixture of ranges and discrete values in find conditions. Paired with Joey Schoblaska.
This commit is contained in:
@@ -22,21 +22,23 @@ module ActiveRecord
|
||||
value = value.select(value.klass.arel_table[value.klass.primary_key]) if value.select_values.empty?
|
||||
attribute.in(value.arel.ast)
|
||||
when Array, ActiveRecord::Associations::CollectionProxy
|
||||
values = value.to_a.map { |x|
|
||||
x.is_a?(ActiveRecord::Base) ? x.id : x
|
||||
}
|
||||
values = value.to_a.map {|x| x.is_a?(ActiveRecord::Base) ? x.id : x}
|
||||
ranges, values = values.partition {|value| value.is_a?(Range) || value.is_a?(Arel::Relation)}
|
||||
|
||||
array_predicates = ranges.map {|range| attribute.in(range)}
|
||||
|
||||
if values.include?(nil)
|
||||
values = values.compact
|
||||
if values.empty?
|
||||
attribute.eq nil
|
||||
array_predicates << attribute.eq(nil)
|
||||
else
|
||||
attribute.in(values.compact).or attribute.eq(nil)
|
||||
array_predicates << attribute.in(values.compact).or(attribute.eq(nil))
|
||||
end
|
||||
else
|
||||
attribute.in(values)
|
||||
array_predicates << attribute.in(values)
|
||||
end
|
||||
|
||||
array_predicates.inject {|composite, predicate| composite.or(predicate)}
|
||||
when Range, Arel::Relation
|
||||
attribute.in(value)
|
||||
when ActiveRecord::Base
|
||||
|
||||
@@ -375,7 +375,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||
end
|
||||
|
||||
def test_find_on_hash_conditions_with_array_of_integers_and_ranges
|
||||
assert_equal [1,2,3], Comment.find(:all, :conditions => { :post_id => [1..2, 3, 4, 5..10]}).map(&:id).sort
|
||||
assert_equal [1,2,3,5,6,7,8,9], Comment.find(:all, :conditions => {:id => [1..2, 3, 5, 6..8, 9]}).map(&:id).sort
|
||||
end
|
||||
|
||||
def test_find_on_multiple_hash_conditions
|
||||
|
||||
Reference in New Issue
Block a user