Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix multiple hash preloads. Fixes #14994

  • Loading branch information...
commit 116c23d1e6315e771ce97dcbef8a251f792576c9 1 parent f88d087
@tenderlove tenderlove authored
View
13 activerecord/lib/active_record/associations/preloader.rb
@@ -112,13 +112,14 @@ def preloaders_on(association, records, scope)
end
def preloaders_for_hash(association, records, scope)
- parent, child = association.to_a.first # hash should only be of length 1
+ association.flat_map { |parent, child|
+ loaders = preloaders_for_one parent, records, scope
- loaders = preloaders_for_one parent, records, scope
-
- recs = loaders.flat_map(&:preloaded_records).uniq
- loaders.concat Array.wrap(child).flat_map { |assoc|
- preloaders_on assoc, recs, scope
+ recs = loaders.flat_map(&:preloaded_records).uniq
+ loaders.concat Array.wrap(child).flat_map { |assoc|
+ preloaders_on assoc, recs, scope
+ }
+ loaders
}
end
View
7 activerecord/test/cases/relations_test.rb
@@ -484,6 +484,13 @@ def test_find_with_preloaded_associations
end
end
+ def test_deep_preload
+ post = Post.preload(author: :posts, comments: :post).first
+
+ assert_predicate post.author.association(:posts), :loaded?
+ assert_predicate post.comments.first.association(:post), :loaded?
+ end
+
def test_preload_applies_to_all_chained_preloaded_scopes
assert_queries(3) do
post = Post.with_comments.with_tags.first
Please sign in to comment.
Something went wrong with that request. Please try again.