aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-11-06 06:40:36 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-11-06 06:40:36 +0900
commite0bef22665f93e88f6b2f3ac6bd55543ed0d6343 (patch)
tree9c36c394d213083d18ef30885b42bdf9500b07bd /activerecord
parente617fb57f5a388d5f0a47fd5e576588dd10066b0 (diff)
downloadrails-e0bef22665f93e88f6b2f3ac6bd55543ed0d6343.tar.gz
rails-e0bef22665f93e88f6b2f3ac6bd55543ed0d6343.tar.bz2
rails-e0bef22665f93e88f6b2f3ac6bd55543ed0d6343.zip
Fix preloading polymorphic multi-level through association
This is partially fixed by e617fb57 when through association has already loaded. Otherwise, second level through association should respect `preload_scope`. Fixes #30242. Closes #30076. [Ryuta Kamizono & CicholGricenchos]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb8
-rw-r--r--activerecord/test/cases/associations/nested_through_associations_test.rb11
2 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index 5bd49b041a..b16fca7dc9 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -40,7 +40,11 @@ module ActiveRecord
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 @@ module ActiveRecord
rhs_records
end
end
+ end.tap do
+ reset_association(middle_records, source_reflection.name, preload_scope)
end
end
diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb
index 11ee45917f..65d30d011b 100644
--- a/activerecord/test/cases/associations/nested_through_associations_test.rb
+++ b/activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -579,6 +579,17 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
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)