1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39156 from bogdan/preloader-duplicate-object-ids

Ensure array passed to preloader has no duplicate records by object_id
This commit is contained in:
Ryuta Kamizono 2020-05-06 02:48:48 +09:00 committed by GitHub
commit 42daf01958
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View file

@ -6,7 +6,7 @@ module ActiveRecord
class Association #:nodoc: class Association #:nodoc:
def initialize(klass, owners, reflection, preload_scope) def initialize(klass, owners, reflection, preload_scope)
@klass = klass @klass = klass
@owners = owners @owners = owners.uniq(&:__id__)
@reflection = reflection @reflection = reflection
@preload_scope = preload_scope @preload_scope = preload_scope
@model = owners.first && owners.first.class @model = owners.first && owners.first.class

View file

@ -41,7 +41,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags, fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags,
:owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses, :owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses,
:subscribers, :books, :subscriptions, :developers, :categorizations, :essays, :subscribers, :books, :subscriptions, :developers, :categorizations, :essays,
:categories_posts, :clubs, :memberships, :organizations :categories_posts, :clubs, :memberships, :organizations, :author_favorites
# Dummies to force column loads so query counts are clean. # Dummies to force column loads so query counts are clean.
def setup def setup
@ -67,11 +67,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end end
def test_preload_with_nested_association def test_preload_with_nested_association
posts = Post.preload(:author, :author_favorites_with_scope).to_a posts = Post.where(id: [authors(:david).id, authors(:mary).id]).
preload(:author, :author_favorites_with_scope).order(:id).to_a
assert_no_queries do assert_no_queries do
posts.each(&:author) posts.each(&:author)
posts.each(&:author_favorites_with_scope) posts.each(&:author_favorites_with_scope)
assert_equal 1, posts[0].author_favorites_with_scope.length
end end
end end