aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorthedarkone <thedarkone2@gmail.com>2014-03-30 20:23:50 +0200
committerthedarkone <thedarkone2@gmail.com>2014-03-30 20:23:50 +0200
commitde32d972bfde8871f7c1a4621f2223ea9b6715b2 (patch)
treebff561681218b1362952fd93151f6b2833e7dbc0 /activerecord
parent593e6978656e3dd07683d2add186b72c9fbad683 (diff)
downloadrails-de32d972bfde8871f7c1a4621f2223ea9b6715b2.tar.gz
rails-de32d972bfde8871f7c1a4621f2223ea9b6715b2.tar.bz2
rails-de32d972bfde8871f7c1a4621f2223ea9b6715b2.zip
Fix polymorphic preloads on NOT NULL _type columns.
Defer to Association#klass instead of having a custom/duplicate code.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader.rb11
-rw-r--r--activerecord/test/cases/associations/eager_test.rb11
2 files changed, 12 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb
index 83637a0409..e6485b2b0c 100644
--- a/activerecord/lib/active_record/associations/preloader.rb
+++ b/activerecord/lib/active_record/associations/preloader.rb
@@ -144,7 +144,7 @@ module ActiveRecord
reflection_records.each_with_object({}) do |(reflection, r_records),h|
h[reflection] = r_records.group_by { |record|
- association_klass(reflection, record)
+ record.association(association).klass
}
end
end
@@ -163,15 +163,6 @@ module ActiveRecord
"perhaps you misspelled it?"
end
- def association_klass(reflection, record)
- if reflection.macro == :belongs_to && reflection.options[:polymorphic]
- klass = record.read_attribute(reflection.foreign_type.to_s)
- klass && klass.constantize
- else
- reflection.klass
- end
- end
-
class AlreadyLoaded
attr_reader :owners, :reflection
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index e59161fc5b..d594697280 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -235,6 +235,17 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
+ def test_finding_with_includes_on_empty_polymorphic_type_column
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
+ sponsor.update!(sponsorable_type: '', sponsorable_id: nil) # sponsorable_type column might be declared NOT NULL
+ sponsor = assert_queries(1) do
+ assert_nothing_raised { Sponsor.all.merge!(:includes => :sponsorable).find(sponsor.id) }
+ end
+ assert_no_queries do
+ assert_equal nil, sponsor.sponsorable
+ end
+ end
+
def test_loading_from_an_association
posts = authors(:david).posts.merge(:includes => :comments, :order => "posts.id").to_a
assert_equal 2, posts.first.comments.size