Skip to content

Commit

Permalink
Don't clone associations [#4894 state:resolved]
Browse files Browse the repository at this point in the history
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 <jose.valim@gmail.com>
  • Loading branch information
paulgillard authored and josevalim committed Jun 23, 2010
1 parent ef7a04e commit d132dd3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1433,6 +1433,7 @@ def initialize_copy(other)
end

clear_aggregation_cache
clear_association_cache
@attributes_cache = {}
@new_record = true
ensure_proper_type
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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 }
Expand Down

0 comments on commit d132dd3

Please sign in to comment.