Skip to content

Commit

Permalink
fix multiple hash preloads. Fixes #14994
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 20, 2014
1 parent f88d087 commit 116c23d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 7 additions & 6 deletions activerecord/lib/active_record/associations/preloader.rb
Expand Up @@ -112,13 +112,14 @@ def preloaders_on(association, records, scope)
end end


def preloaders_for_hash(association, records, scope) 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|
recs = loaders.flat_map(&:preloaded_records).uniq preloaders_on assoc, recs, scope
loaders.concat Array.wrap(child).flat_map { |assoc| }
preloaders_on assoc, recs, scope loaders
} }
end end


Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -484,6 +484,13 @@ def test_find_with_preloaded_associations
end end
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 def test_preload_applies_to_all_chained_preloaded_scopes
assert_queries(3) do assert_queries(3) do
post = Post.with_comments.with_tags.first post = Post.with_comments.with_tags.first
Expand Down

0 comments on commit 116c23d

Please sign in to comment.