aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-02-28 20:42:01 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-02-28 20:42:01 +0000
commit5dee6ce9e0e31c86c4319c17a1ee551832ecb7a8 (patch)
treea56b21361b4135dfe7626c471000afc574e21110 /activerecord/lib
parentd6e355d1dfe483b93c0d37b74e8ad8405a1b7505 (diff)
downloadrails-5dee6ce9e0e31c86c4319c17a1ee551832ecb7a8.tar.gz
rails-5dee6ce9e0e31c86c4319c17a1ee551832ecb7a8.tar.bz2
rails-5dee6ce9e0e31c86c4319c17a1ee551832ecb7a8.zip
Fix that batched :include would pull in duplicate records in some cases. Closes #11215 [Catfish]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8942 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/association_preload.rb2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
2 files changed, 5 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index a89f5f5459..cc657dc433 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -10,7 +10,7 @@ module ActiveRecord
# 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)}
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index cfdd33d6e8..5162f859aa 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1536,8 +1536,10 @@ module ActiveRecord
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?