提交 5e75f516 编写于 作者: Y Yves Senn

Merge pull request #20552 from jamesdabbs/belongs-to-polymorphic-force-reload

Fix `undefined method uncached` for polymorphic belongs_to #20426
* Prevent error when using `force_reload: true` on an unassigned polymorphic
belongs_to association.
Fixes #20426.
*James Dabbs*
* Correctly raise `ActiveRecord::AssociationTypeMismatch` when assigning * Correctly raise `ActiveRecord::AssociationTypeMismatch` when assigning
a wrong type to a namespaced association. a wrong type to a namespaced association.
......
...@@ -3,7 +3,7 @@ module Associations ...@@ -3,7 +3,7 @@ module Associations
class SingularAssociation < Association #:nodoc: class SingularAssociation < Association #:nodoc:
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar # Implements the reader method, e.g. foo.bar for Foo.has_one :bar
def reader(force_reload = false) def reader(force_reload = false)
if force_reload if force_reload && klass
klass.uncached { reload } klass.uncached { reload }
elsif !loaded? || stale_target? elsif !loaded? || stale_target?
reload reload
......
...@@ -318,9 +318,11 @@ def test_with_condition ...@@ -318,9 +318,11 @@ def test_with_condition
def test_polymorphic_association_class def test_polymorphic_association_class
sponsor = Sponsor.new sponsor = Sponsor.new
assert_nil sponsor.association(:sponsorable).send(:klass) assert_nil sponsor.association(:sponsorable).send(:klass)
assert_nil sponsor.sponsorable(force_reload: true)
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
assert_nil sponsor.association(:sponsorable).send(:klass) assert_nil sponsor.association(:sponsorable).send(:klass)
assert_nil sponsor.sponsorable(force_reload: true)
sponsor.sponsorable = Member.new :name => "Bert" sponsor.sponsorable = Member.new :name => "Bert"
assert_equal Member, sponsor.association(:sponsorable).send(:klass) assert_equal Member, sponsor.association(:sponsorable).send(:klass)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册