Skip to content

Commit

Permalink
Fix undefined method uncached for polymorphic belongs_to #20426
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamesdabbs committed Jun 13, 2015
1 parent 3692ca5 commit 5a9590c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
Expand Up @@ -3,7 +3,7 @@ module Associations
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
Expand Down
Expand Up @@ -288,6 +288,7 @@ def test_with_condition
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)
Expand Down

0 comments on commit 5a9590c

Please sign in to comment.