Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix that batched :include would pull in duplicate records in some cas…

…es. Closes #11215 [Catfish]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8942 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 5dee6ce9e0e31c86c4319c17a1ee551832ecb7a8 1 parent d6e355d
@jeremy jeremy authored
View
2  activerecord/lib/active_record/association_preload.rb
@@ -10,7 +10,7 @@ module ClassMethods
# preload_options is passed only one level deep: don't pass to the child associations when associations is a Hash
protected
def preload_associations(records, associations, preload_options={})
- records = [records].flatten.compact
+ records = [records].flatten.compact.uniq
return if records.empty?
case associations
when Array then associations.each {|association| preload_associations(records, association, preload_options)}
View
6 activerecord/lib/active_record/associations.rb
@@ -1536,8 +1536,10 @@ def remove_duplicate_results!(base, records, associations)
is_collection = [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
parent_records = records.map do |record|
- next unless record.send(reflection.name)
- is_collection ? record.send(reflection.name).target.uniq! : record.send(reflection.name)
+ descendant = record.send(reflection.name)
+ next unless descendant
+ descendant.target.uniq! if is_collection
+ descendant
end.flatten.compact
remove_duplicate_results!(reflection.class_name.constantize, parent_records, associations[name]) unless parent_records.empty?
View
26 activerecord/test/cases/associations/eager_test.rb
@@ -67,6 +67,32 @@ def test_duplicate_middle_objects
end
end
+ def test_including_duplicate_objects_from_belongs_to
+ popular_post = Post.create!(:body => "I like cars!")
+ comment = popular_post.comments.create!(:body => "lol")
+ popular_post.readers.create!(:person => people(:michael))
+ popular_post.readers.create!(:person => people(:david))
+
+ readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
+ :include => {:post => :comments})
+ readers.each do |reader|
+ assert_equal [comment], reader.post.comments
+ end
+ end
+
+ def test_including_duplicate_objects_from_has_many
+ car_post = Post.create!(:body => "I like cars!")
+ car_post.categories << categories(:general)
+ car_post.categories << categories(:technology)
+
+ comment = car_post.comments.create!(:body => "hmm")
+ categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
+ :include => {:posts => :comments})
+ categories.each do |category|
+ assert_equal [comment], category.posts[0].comments
+ end
+ end
+
def test_loading_from_an_association
posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
assert_equal 2, posts.first.comments.size
View
5 activerecord/test/fixtures/people.yml
@@ -1,3 +1,6 @@
michael:
id: 1
- first_name: Michael
+ first_name: Michael
+david:
+ id: 2
+ first_name: David
Please sign in to comment.
Something went wrong with that request. Please try again.