Skip to content

Commit 5a9590c

Browse files
committed
Fix undefined method uncached for polymorphic belongs_to rails#20426
Unitialized polymorphic `belongs_to` associations raise an error while attempting to reload, as they attempt to make an uncached reload, but don't have a klass to fetch uncachedly. In this case, `loaded?` should be `false` anyway.
1 parent 3692ca5 commit 5a9590c

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def test_with_condition
288288
def test_polymorphic_association_class
289289
sponsor = Sponsor.new
290290
assert_nil sponsor.association(:sponsorable).send(:klass)
291+
assert_nil sponsor.sponsorable(force_reload: true)
291292

292293
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
293294
assert_nil sponsor.association(:sponsorable).send(:klass)

0 commit comments

Comments
 (0)