mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #37457 from sinsoku/fix_issue_37446
Fix an issue with duplicate preloaded records
This commit is contained in:
commit
c7cc6aa106
2 changed files with 11 additions and 1 deletions
|
@ -111,7 +111,7 @@ module ActiveRecord
|
||||||
association.flat_map { |parent, child|
|
association.flat_map { |parent, child|
|
||||||
grouped_records(parent, records, polymorphic_parent).flat_map do |reflection, reflection_records|
|
grouped_records(parent, records, polymorphic_parent).flat_map do |reflection, reflection_records|
|
||||||
loaders = preloaders_for_reflection(reflection, reflection_records, scope)
|
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]
|
child_polymorphic_parent = reflection && reflection.options[:polymorphic]
|
||||||
loaders.concat Array.wrap(child).flat_map { |assoc|
|
loaders.concat Array.wrap(child).flat_map { |assoc|
|
||||||
preloaders_on assoc, recs, scope, child_polymorphic_parent
|
preloaders_on assoc, recs, scope, child_polymorphic_parent
|
||||||
|
|
|
@ -190,4 +190,14 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
||||||
assert_equal 3, authors[1].posts.size
|
assert_equal 3, authors[1].posts.size
|
||||||
assert_equal 3, authors[0].posts.collect { |post| post.categorizations.size }.inject(0) { |sum, i| sum + i }
|
assert_equal 3, authors[0].posts.collect { |post| post.categorizations.size }.inject(0) { |sum, i| sum + i }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue