Skip to content

Commit 5e75f51

Browse files
committed
Merge pull request rails#20552 from jamesdabbs/belongs-to-polymorphic-force-reload
Fix `undefined method uncached` for polymorphic belongs_to rails#20426
2 parents 8e27fd9 + 5a9590c commit 5e75f51

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Prevent error when using `force_reload: true` on an unassigned polymorphic
2+
belongs_to association.
3+
4+
Fixes #20426.
5+
6+
*James Dabbs*
7+
18
* Correctly raise `ActiveRecord::AssociationTypeMismatch` when assigning
29
a wrong type to a namespaced association.
310

activerecord/lib/active_record/associations/singular_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Associations
33
class SingularAssociation < Association #:nodoc:
44
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar
55
def reader(force_reload = false)
6-
if force_reload
6+
if force_reload && klass
77
klass.uncached { reload }
88
elsif !loaded? || stale_target?
99
reload

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ def test_with_condition
318318
def test_polymorphic_association_class
319319
sponsor = Sponsor.new
320320
assert_nil sponsor.association(:sponsorable).send(:klass)
321+
assert_nil sponsor.sponsorable(force_reload: true)
321322

322323
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
323324
assert_nil sponsor.association(:sponsorable).send(:klass)
325+
assert_nil sponsor.sponsorable(force_reload: true)
324326

325327
sponsor.sponsorable = Member.new :name => "Bert"
326328
assert_equal Member, sponsor.association(:sponsorable).send(:klass)

0 commit comments

Comments
 (0)