mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20552 from jamesdabbs/belongs-to-polymorphic-force-reload
Fix `undefined method uncached` for polymorphic belongs_to #20426
This commit is contained in:
commit
5e75f51692
3 changed files with 10 additions and 1 deletions
|
@ -1,3 +1,10 @@
|
|||
* Prevent error when using `force_reload: true` on an unassigned polymorphic
|
||||
belongs_to association.
|
||||
|
||||
Fixes #20426.
|
||||
|
||||
*James Dabbs*
|
||||
|
||||
* Correctly raise `ActiveRecord::AssociationTypeMismatch` when assigning
|
||||
a wrong type to a namespaced association.
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ module ActiveRecord
|
|||
class SingularAssociation < Association #:nodoc:
|
||||
# Implements the reader method, e.g. foo.bar for Foo.has_one :bar
|
||||
def reader(force_reload = false)
|
||||
if force_reload
|
||||
if force_reload && klass
|
||||
klass.uncached { reload }
|
||||
elsif !loaded? || stale_target?
|
||||
reload
|
||||
|
|
|
@ -318,9 +318,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
|||
def test_polymorphic_association_class
|
||||
sponsor = Sponsor.new
|
||||
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
|
||||
assert_nil sponsor.association(:sponsorable).send(:klass)
|
||||
assert_nil sponsor.sponsorable(force_reload: true)
|
||||
|
||||
sponsor.sponsorable = Member.new :name => "Bert"
|
||||
assert_equal Member, sponsor.association(:sponsorable).send(:klass)
|
||||
|
|
Loading…
Reference in a new issue