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:
commit
42daf01958
2 changed files with 5 additions and 3 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue