Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #11945 from Mik-die/polymorphic-decorator

check class hierarchy with is_a? in PredicateBuilder.expand
  • Loading branch information...
commit a10e2f17cac3aec0ae309a501fab3384e8bfe99c 2 parents 182f1d1 + c2e084a
@rafaelfranca rafaelfranca authored
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Fix PredicateBuilder so polymorhic association keys in `where` clause can
+ also accept not only `ActiveRecord::Base` direct descendances (decorated
+ models, for example).
+
+ *Mikhail Dieterle*
+
* PostgreSQL adapter recognizes negative money values formatted with
parentheses (eg. `($1.25) # => -1.25`)).
Fixes #11899.
View
2  activerecord/lib/active_record/relation/predicate_builder.rb
@@ -55,7 +55,7 @@ def self.expand(klass, table, column, value)
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(estimate_of: treasure)
- if klass && value.class < Base && reflection = klass.reflect_on_association(column.to_sym)
+ if klass && value.is_a?(Base) && reflection = klass.reflect_on_association(column.to_sym)
if reflection.polymorphic?
queries << build(table[reflection.foreign_type], value.class.base_class)
end
View
25 activerecord/test/cases/relation/where_test.rb
@@ -81,6 +81,31 @@ def test_polymorphic_sti_nested_where
assert_equal expected.to_sql, actual.to_sql
end
+ def test_decorated_polymorphic_where
+ treasure_decorator = Struct.new(:model) do
+ def self.method_missing(method, *args, &block)
+ Treasure.send(method, *args, &block)
+ end
+
+ def is_a?(klass)
+ model.is_a?(klass)
+ end
+
+ def method_missing(method, *args, &block)
+ model.send(method, *args, &block)
+ end
+ end
+
+ treasure = Treasure.new
+ treasure.id = 1
+ decorated_treasure = treasure_decorator.new(treasure)
+
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: decorated_treasure)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
def test_aliased_attribute
expected = Topic.where(heading: 'The First Topic')
actual = Topic.where(title: 'The First Topic')
Please sign in to comment.
Something went wrong with that request. Please try again.