From e08372272de4c03d783bc11cb84b79a5a31ebf32 Mon Sep 17 00:00:00 2001 From: Paul Gillard Date: Fri, 18 Jun 2010 00:02:31 +0100 Subject: [PATCH] Don't clone associations [#4894 state:resolved] Cloning an active record object should be shallow in that it should copy attributes but not associations. This was no longer true as a result of #3164. --- activerecord/lib/active_record/base.rb | 1 + activerecord/test/cases/base_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3b6ffa4..03e7f77 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1460,6 +1460,7 @@ module ActiveRecord #:nodoc: end clear_aggregation_cache + clear_association_cache @attributes_cache = {} @new_record = true ensure_proper_type diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 5c175de..fa4f6b7 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1401,6 +1401,14 @@ class BasicsTest < ActiveRecord::TestCase assert_not_equal clone.id, dev.id end + def test_clone_does_not_clone_associations + author = authors(:david) + assert_not_equal [], author.posts + + author_clone = author.clone + assert_equal [], author_clone.posts + end + def test_clone_preserves_subtype clone = nil assert_nothing_raised { clone = Company.find(3).clone } -- 1.6.5.1