This project is archived and is in readonly mode.
Instantiate through initialize_copy when cloning
Reported by Paul Gillard | September 8th, 2009 @ 10:49 AM
As discussed here I'm attaching a patch which overrides #initialize_copy rather than #clone in order to implement cloning of AR objects. This will allow AR classes to implement deep cloning of attributes much more easily, for example:
  def initialize_copy(other)
    super
    self.an_association = other.an_association
  end
This patch isn't intended to be the finished article as I have two particular points on which I'd appreciate some feedback.
Firstly, I'm having to call the after_initialize callback near the top of #initialize_copy in order to get the test suite to pass. This doesn't seem right to me and I've documented my thoughts in the patch file.
Secondly, #initialize allows a block to be passed in. Should #initialize_copy do a similar thing? If so I'll add a test and relevant code.
Comments and changes to this ticket
- 
            
        

Paul Gillard October 6th, 2009 @ 06:17 PM
It would seem my second point is not a valid one. #initialize_copy only gets called through #dup and #clone and neither accept a block.
 - 
        

Michael Koziarski October 6th, 2009 @ 08:40 PM
- Assigned user set to josh
 
 - 
        

Repository October 6th, 2009 @ 10:26 PM
- State changed from new to resolved
 
(from [6361d4234ca7f7c2dcb98e6ed34187d2933b56d7]) Call initialize_copy when cloning [#3164 state:resolved]
Cloned AR objects are now instantiated through initialize_copy rather than
new/initialize. This allows AR classes to override initialize_copy in order to
implement deep cloning.Signed-off-by: Joshua Peek josh@joshpeek.com
http://github.com/rails/rails/commit/6361d4234ca7f7c2dcb98e6ed34187... - 
            
        

 
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>
People watching this ticket
Attachments
Tags
Referenced by
- 
        
          4538 
          ActiveRecord.clone does not clear out timestamps
        Is this an acceptable solution? If not, I'll have to rein...
         - 
        
          3164 
          Instantiate through initialize_copy when cloning
        (from [6361d4234ca7f7c2dcb98e6ed34187d2933b56d7])
Call in...
         - 
        
          4894 
          Record Cloning Not Shallow
        Roughly speaking Ruby clones objects by copying instance
...
         - 
        
          4894 
          Record Cloning Not Shallow
        Roughly speaking Ruby clones objects by copying instance
...
         - 
        
          4894 
          Record Cloning Not Shallow
        I attach two patches. One which clears the association ca...
         - 
        
          4894 
          Record Cloning Not Shallow
        Cloning an active record object should be shallow in that...
         - 
        
          4894 
          Record Cloning Not Shallow
        If you're happy to, could you also commit my second patch...