From d132dd33520ba61f7bfa9ba6fdd1b7b2bebd27f3 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] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 1 + activerecord/test/cases/base_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3f81ca7555743..3a7db97a6ae78 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1433,6 +1433,7 @@ def initialize_copy(other) 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 7c74d87b61226..c24a494c85df2 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1401,6 +1401,14 @@ def test_clone_with_aggregate_of_same_name_as_attribute 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 }