Forum: Ruby on Rails Clone ActiveRecords

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4b1e1f7d8e6714dcfeeb3b4d1df908a1?d=identicon&s=25 Edwin Meijer (Guest)
on 2007-05-02 18:58
(Received via mailing list)
I am using the Marshal module to create a deep clone of an
ActiveRecord object hierarchy... i.e. an object that has_many
children, which at their turn have many children:

In my model file I have:

  def deep_clone()

    aClone = Marshal::load(Marshal.dump(self))

    return aClone


This works fine, the returned clone contains the same has_many
hierarchy as the original...

However, it also copies the value of the "id" attribute for each of
the cloned objects in the hierarchy... thus if I were to save it (e.g., I am effectively writing it to the same database records

Question: what's the best way to turn these cloned objects in the
hierarchy into a new ActiveRecords that upon save are going to get
created as a set of new records... AND that maintain has_many
relationships between them... is it just a matter of nil'ing out the
id columns ?

4b1e1f7d8e6714dcfeeb3b4d1df908a1?d=identicon&s=25 Edwin Meijer (Guest)
on 2007-05-02 19:33
(Received via mailing list)
I guess setting the id column to nil is not cutting it.. you also need
to set the new_record attribute on each cloned object to false ?
This topic is locked and can not be replied to.