Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make PredicateBuilder recognise AR::Model

  • Loading branch information...
commit 35202aa6911679cccb8187d12996e3f30661385c 1 parent a1ee3ac
@jonleighton jonleighton authored
View
4 activerecord/lib/active_record/relation/predicate_builder.rb
@@ -22,7 +22,7 @@ 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::Model) ? x.id : x}
ranges, values = values.partition {|v| v.is_a?(Range) || v.is_a?(Arel::Relation)}
array_predicates = ranges.map {|range| attribute.in(range)}
@@ -41,7 +41,7 @@ def self.build_from_hash(engine, attributes, default_table)
array_predicates.inject {|composite, predicate| composite.or(predicate)}
when Range, Arel::Relation
attribute.in(value)
- when ActiveRecord::Base
+ when ActiveRecord::Model
attribute.eq(value.id)
when Class
# FIXME: I think we need to deprecate this behavior
View
6 activerecord/test/cases/inclusion_test.rb
@@ -23,6 +23,12 @@ def test_exists
t = Teapot.create!(:name => "Ronnie Kemper")
assert Teapot.exists?(t)
end
+
+ def test_predicate_builder
+ t = Teapot.create!(:name => "Bob")
+ assert_equal "Bob", Teapot.where(:id => [t]).first.name
+ assert_equal "Bob", Teapot.where(:id => t).first.name
+ end
end
class InclusionUnitTest < ActiveRecord::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.