Permalink
Browse files

Fix preloading polymorphic multi-level through association

This is partially fixed by e617fb5 when through association has already
loaded. Otherwise, second level through association should respect
`preload_scope`.

Fixes #30242.
Closes #30076.

[Ryuta Kamizono & CicholGricenchos]
  • Loading branch information...
kamipo committed Nov 5, 2017
1 parent e617fb5 commit e0bef22665f93e88f6b2f3ac6bd55543ed0d6343
@@ -40,7 +40,11 @@ def associated_records_by_owner(preloader)
middle_records = through_records.flat_map(&:last)
reflection_scope = reflection_scope() if reflection.scope
if preload_scope
reflection_scope = reflection_scope().merge(preload_scope)
elsif reflection.scope
reflection_scope = reflection_scope()
end
preloaders = preloader.preload(middle_records,
source_reflection.name,
@@ -70,6 +74,8 @@ def associated_records_by_owner(preloader)
rhs_records
end
end
end.tap do

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Nov 25, 2017

Member

This tap is confusing. Maybe store the result in a local variable and return it later?

@rafaelfranca

rafaelfranca Nov 25, 2017

Member

This tap is confusing. Maybe store the result in a local variable and return it later?

This comment has been minimized.

Show comment
Hide comment
@kamipo

kamipo Nov 26, 2017

Member

This tap was refactored in #22115.

@kamipo

kamipo Nov 26, 2017

Member

This tap was refactored in #22115.

reset_association(middle_records, source_reflection.name, preload_scope)
end
end
@@ -579,6 +579,17 @@ def test_nested_has_many_through_should_not_be_autosaved
assert !c.post_taggings.empty?
end
def test_polymorphic_has_many_through_when_through_association_has_not_loaded
cake_designer = CakeDesigner.create!(chef: Chef.new)
drink_designer = DrinkDesigner.create!(chef: Chef.new)
department = Department.create!(chefs: [cake_designer.chef, drink_designer.chef])
Hotel.create!(departments: [department])
hotel = Hotel.includes(:cake_designers, :drink_designers).take
assert_equal [cake_designer], hotel.cake_designers
assert_equal [drink_designer], hotel.drink_designers
end
def test_polymorphic_has_many_through_when_through_association_has_already_loaded
cake_designer = CakeDesigner.create!(chef: Chef.new)
drink_designer = DrinkDesigner.create!(chef: Chef.new)

0 comments on commit e0bef22

Please sign in to comment.