aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2019-03-06 13:47:25 +0200
committerBogdan Gusiev <agresso@gmail.com>2019-03-07 16:48:46 +0200
commit2847653869ffc1ff5139c46e520c72e26618c199 (patch)
tree51199c44ed1b6909fc28353d3a1245a1e2a0f849 /activerecord/test/cases/associations
parentb366be3b5b28f01c8a55d67a5161ec36f53d555c (diff)
downloadrails-2847653869ffc1ff5139c46e520c72e26618c199.tar.gz
rails-2847653869ffc1ff5139c46e520c72e26618c199.tar.bz2
rails-2847653869ffc1ff5139c46e520c72e26618c199.zip
Fix preloader to never reset associations in case they are already loaded
This patch fixes the issue when association is preloaded with a custom preload scope which disposes the already preloaded target of the association by reseting it. When custom preload scope is used, the preloading is now performed into a separated Hash - #records_by_owner instead of the association. It removes the necessaty the reset the association after the preloading is complete so that reset of the preloaded association never happens. Preloading is still happening to the association when the preload scope is empty.
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r--activerecord/test/cases/associations/nested_through_associations_test.rb9
1 files changed, 9 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb
index 0b83fd8421..35da74102d 100644
--- a/activerecord/test/cases/associations/nested_through_associations_test.rb
+++ b/activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -548,6 +548,15 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_through_association_preload_doesnt_reset_source_association_if_already_preloaded
+ blue = tags(:blue)
+ authors = Author.preload(posts: :first_blue_tags_2, misc_post_first_blue_tags_2: {}).to_a.sort_by(&:id)
+
+ assert_no_queries do
+ assert_equal [blue], authors[2].posts.first.first_blue_tags_2
+ end
+ end
+
def test_nested_has_many_through_with_conditions_on_source_associations_preload_via_joins
# Pointless condition to force single-query loading
assert_includes_and_joins_equal(