Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

Instantiate through initialize_copy when cloning

#3164

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.

Reported by Paul Gillard · September 8th, 2009 @ 10:49 AM

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. Paul Gillard
    Paul Gillard

    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.

    October 6th, 2009 @ 06:17 PM

  2. Michael Koziarski
  3. Repository
    Repository
    • State changed from new to resolved

    (from [6361d4234ca7f7c2dcb98e6ed34187d2933b56d7]) Call initialize_copy when cloning [#3164 Instantiate through initialize_copy when cloning 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...

    October 6th, 2009 @ 10:26 PM

  4. bingbing