mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fix problem with duplicated records when a :uniq :through association is preloaded [#2447 state:resolved]
This commit is contained in:
committed by
Aaron Patterson
parent
6bfa846a2c
commit
09ddca67ac
@@ -251,6 +251,7 @@ module ActiveRecord
|
||||
through_record_id = through_record[reflection.through_reflection_primary_key].to_s
|
||||
add_preloaded_records_to_collection(id_to_record_map[through_record_id], reflection.name, through_record.send(source))
|
||||
end
|
||||
records.each { |record| record.send(reflection.name).target.uniq! } if options[:uniq]
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
@@ -20,10 +20,11 @@ require 'models/project'
|
||||
require 'models/member'
|
||||
require 'models/membership'
|
||||
require 'models/club'
|
||||
require 'models/categorization'
|
||||
|
||||
class EagerAssociationTest < ActiveRecord::TestCase
|
||||
fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
|
||||
:companies, :accounts, :tags, :taggings, :people, :readers,
|
||||
:companies, :accounts, :tags, :taggings, :people, :readers, :categorizations,
|
||||
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
|
||||
:developers, :projects, :developers_projects, :members, :memberships, :clubs
|
||||
|
||||
@@ -910,4 +911,10 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
||||
assert_queries(2) { @tagging = Tagging.preload(:taggable).find(t.id) }
|
||||
assert_no_queries { assert ! @tagging.taggable }
|
||||
end
|
||||
|
||||
def test_preloading_has_many_through_with_uniq
|
||||
mary = Author.includes(:unique_categorized_posts).where(:id => authors(:mary).id).first
|
||||
assert_equal 1, mary.unique_categorized_posts.length
|
||||
assert_equal 1, mary.unique_categorized_post_ids.length
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,7 +23,7 @@ require 'models/category'
|
||||
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
fixtures :posts, :readers, :people, :comments, :authors,
|
||||
:owners, :pets, :toys, :jobs, :references, :companies,
|
||||
:subscribers, :books, :subscriptions, :developers
|
||||
:subscribers, :books, :subscriptions, :developers, :categorizations
|
||||
|
||||
# Dummies to force column loads so query counts are clean.
|
||||
def setup
|
||||
@@ -473,4 +473,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
category = authors(:david).special_categories.create(:name => "Foo")
|
||||
assert_equal 1, category.categorizations.where(:special => true).count
|
||||
end
|
||||
|
||||
def test_joining_has_many_through_with_uniq
|
||||
mary = Author.joins(:unique_categorized_posts).where(:id => authors(:mary).id).first
|
||||
assert_equal 1, mary.unique_categorized_posts.length
|
||||
assert_equal 1, mary.unique_categorized_post_ids.length
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user