Skip to content
This repository
Browse code

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.)
  • Loading branch information...
commit 8987cda2bb64fe289f7a28b2833a2786cfbc8259 1 parent af27262
Jon Leighton authored March 04, 2011 tenderlove committed March 04, 2011
6  activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
@@ -31,10 +31,12 @@ def association_key
31 31
         private
32 32
 
33 33
         # Once we have used the join table column (in super), we manually instantiate the
34  
-        # actual records
  34
+        # actual records, ensuring that we don't create more than one instances of the same
  35
+        # record
35 36
         def associated_records_by_owner
  37
+          records = {}
36 38
           super.each do |owner_key, rows|
37  
-            rows.map! { |row| klass.instantiate(row) }
  39
+            rows.map! { |row| records[row[klass.primary_key]] ||= klass.instantiate(row) }
38 40
           end
39 41
         end
40 42
 
16  activerecord/test/cases/associations/eager_test.rb
@@ -525,6 +525,22 @@ def test_eager_with_has_and_belongs_to_many_and_limit
525 525
     assert posts[1].categories.include?(categories(:general))
526 526
   end
527 527
 
  528
+  # This is only really relevant when the identity map is off. Since the preloader for habtm
  529
+  # gets raw row hashes from the database and then instantiates them, this test ensures that
  530
+  # it only instantiates one actual object per record from the database.
  531
+  def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times
  532
+    welcome    = posts(:welcome)
  533
+    categories = Category.includes(:posts)
  534
+
  535
+    general    = categories.find { |c| c == categories(:general) }
  536
+    technology = categories.find { |c| c == categories(:technology) }
  537
+
  538
+    post1 = general.posts.to_a.find { |p| p == posts(:welcome) }
  539
+    post2 = technology.posts.to_a.find { |p| p == posts(:welcome) }
  540
+
  541
+    assert_equal post1.object_id, post2.object_id
  542
+  end
  543
+
528 544
   def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
529 545
     posts = authors(:david).posts.find(:all,
530 546
       :include    => :comments,

0 notes on commit 8987cda

Please sign in to comment.
Something went wrong with that request. Please try again.