未验证 提交 4b455e4f 编写于 作者: S sinsoku

Fix an issue with duplicate preloaded records

Fixes #37446
上级 cd30ec1f
......@@ -111,7 +111,7 @@ def preloaders_for_hash(association, records, scope, polymorphic_parent)
association.flat_map { |parent, child|
grouped_records(parent, records, polymorphic_parent).flat_map do |reflection, reflection_records|
loaders = preloaders_for_reflection(reflection, reflection_records, scope)
recs = loaders.flat_map(&:preloaded_records)
recs = loaders.flat_map(&:preloaded_records).uniq
child_polymorphic_parent = reflection && reflection.options[:polymorphic]
loaders.concat Array.wrap(child).flat_map { |assoc|
preloaders_on assoc, recs, scope, child_polymorphic_parent
......
......@@ -190,4 +190,15 @@ def test_eager_association_loading_with_cascaded_interdependent_one_level_and_tw
assert_equal 3, authors[1].posts.size
assert_equal 3, authors[0].posts.collect { |post| post.categorizations.size }.inject(0) { |sum, i| sum + i }
end
# Regression test for https://github.com/rails/rails/issues/37446
def test_preloaded_records_are_not_duplicated
author = Author.first
expected = Post.where(author: author)
.includes(author: :first_posts).map { |post| post.author.first_posts.size }
actual = author.posts
.includes(author: :first_posts).map { |post| post.author.first_posts.size }
assert_equal expected, actual
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册