Permalink
Browse files

Fix polymorphic preloads on NOT NULL _type columns.

Defer to Association#klass instead of having a custom/duplicate code.
  • Loading branch information...
thedarkone committed Mar 30, 2014
1 parent 593e697 commit de32d972bfde8871f7c1a4621f2223ea9b6715b2
@@ -144,7 +144,7 @@ def grouped_records(association, records)
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 @@ def raise_config_error(record, association)
"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
@@ -235,6 +235,17 @@ def test_finding_with_includes_on_null_belongs_to_polymorphic_association
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

0 comments on commit de32d97

Please sign in to comment.