aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-03-04 22:29:40 +0000
committerJon Leighton <j@jonathanleighton.com>2011-03-04 22:33:16 +0000
commit73c0b390b3a1ea9487c3f667352463a90af6dd71 (patch)
tree087974be6501368e7877113954d528dec8d7ad4f /activerecord/lib/active_record/associations/preloader
parenta5ef8b9fa0b30f73af206145cfc39228cb6b9526 (diff)
downloadrails-73c0b390b3a1ea9487c3f667352463a90af6dd71.tar.gz
rails-73c0b390b3a1ea9487c3f667352463a90af6dd71.tar.bz2
rails-73c0b390b3a1ea9487c3f667352463a90af6dd71.zip
When preloading has_and_belongs_to_many associations, we should only instantiate one AR object per actual record in the database. (Even when IM is off.)
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader')
-rw-r--r--activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb6
1 files changed, 4 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
index e794f05340..24be279449 100644
--- a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
+++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
@@ -31,10 +31,12 @@ module ActiveRecord
private
# Once we have used the join table column (in super), we manually instantiate the
- # actual records
+ # actual records, ensuring that we don't create more than one instances of the same
+ # record
def associated_records_by_owner
+ records = {}
super.each do |owner_key, rows|
- rows.map! { |row| klass.instantiate(row) }
+ rows.map! { |row| records[row[klass.primary_key]] ||= klass.instantiate(row) }
end
end