diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-06-23 10:04:17 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-06-23 10:19:54 +0200 |
commit | 5e75f51692cc92ad346fecc4a63fa628c8024149 (patch) | |
tree | a481658810f057ea477f8dea7bc64cdcb3934b13 | |
parent | 8e27fd95945ac9f81a5ad1b30e5d6c5f32878afa (diff) | |
parent | 5a9590c6cbbb4ac26a81ae37088a38778cb12748 (diff) | |
download | rails-5e75f51692cc92ad346fecc4a63fa628c8024149.tar.gz rails-5e75f51692cc92ad346fecc4a63fa628c8024149.tar.bz2 rails-5e75f51692cc92ad346fecc4a63fa628c8024149.zip |
Merge pull request #20552 from jamesdabbs/belongs-to-polymorphic-force-reload
Fix `undefined method uncached` for polymorphic belongs_to #20426
-rw-r--r-- | activerecord/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/associations/belongs_to_associations_test.rb | 2 |
3 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 95dd85a387..d87e609956 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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. diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 58d0f7d65d..bec9505bd2 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -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 diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 4a72ec5054..ebe08c618f 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -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) |