This project is archived and is in readonly mode.

#4894 ✓resolved
Paul Gillard

Record Cloning Not Shallow

Reported by Paul Gillard | June 17th, 2010 @ 11:57 PM | in 3.0.2

Problem

Cloned Active Record objects receive the original object's associations.

Explanation

Roughly speaking Ruby clones objects by copying instance variables into a new object. Before #3164 Active Record implemented cloning by defining its own #clone method. This method only copied instance variables that were wanted. Since #3164 Ruby's in-built #initialize_copy gets called and this copies all instance variables. This means that associations are incorrectly copied (as they are stored within records as instance variables).

The Active Record documentation for #initialize_copy states:

Note that this is a "shallow" clone as it copies the object's attributes only, not its associations.

However this is no longer the case.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages