Skip to content

Commit

Permalink
Ensure array passed to preloader has no duplicate records by object_id
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan authored and berowar committed Oct 9, 2020
1 parent fe76a95 commit f59f998
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Preloader
class Association #:nodoc:
def initialize(klass, owners, reflection, preload_scope)
@klass = klass
@owners = owners
@owners = owners.uniq(&:__id__)
@reflection = reflection
@preload_scope = preload_scope
@model = owners.first && owners.first.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags,
:owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses,
: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.
def setup
Expand All @@ -67,11 +67,13 @@ def test_through_association_with_left_joins
end

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
posts.each(&:author)
posts.each(&:author_favorites_with_scope)
assert_equal 1, posts[0].author_favorites_with_scope.length
end
end

Expand Down

0 comments on commit f59f998

Please sign in to comment.