Permalink
Browse files

Fixes issue #3483, regarding using a mixture of ranges and discrete v…

…alues in find conditions. Paired with Joey Schoblaska.
  • Loading branch information...
1 parent e1b79c5 commit 63a22ca6169f06d10cd843ea3a75df6498fdcf7d @RyanNaughton RyanNaughton committed Nov 15, 2011
View
14 activerecord/lib/active_record/relation/predicate_builder.rb
@@ -22,21 +22,23 @@ def self.build_from_hash(engine, attributes, default_table)
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
View
2 activerecord/test/cases/finder_test.rb
@@ -375,7 +375,7 @@ def test_find_on_hash_conditions_with_multiple_ranges
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

0 comments on commit 63a22ca

Please sign in to comment.