aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
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/test
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/test')
-rw-r--r--activerecord/test/cases/associations/eager_test.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index ed6337b596..40c82f2fb8 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -525,6 +525,22 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert posts[1].categories.include?(categories(:general))
end
+ # This is only really relevant when the identity map is off. Since the preloader for habtm
+ # gets raw row hashes from the database and then instantiates them, this test ensures that
+ # it only instantiates one actual object per record from the database.
+ def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times
+ welcome = posts(:welcome)
+ categories = Category.includes(:posts)
+
+ general = categories.find { |c| c == categories(:general) }
+ technology = categories.find { |c| c == categories(:technology) }
+
+ post1 = general.posts.to_a.find { |p| p == posts(:welcome) }
+ post2 = technology.posts.to_a.find { |p| p == posts(:welcome) }
+
+ assert_equal post1.object_id, post2.object_id
+ end
+
def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
posts = authors(:david).posts.find(:all,
:include => :comments,