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
end
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.
aClone.save), 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 ?
Thanks,
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 ? On May 2, 9:57 am, Edwin Meijer <edwin.mei...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > end > > 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. > aClone.save), 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 ? > > Thanks,--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 may have many children, etc.
In model file of the root object I have the following method:
def deep_clone()
aClone = Marshal::load(Marshal.dump(self))
return aClone
end
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... if I were to save it (e.g.
aClone.save), I am effectively updating the same database record as the
original.
Question: what''s the best way to turn these cloned objects in the
hierarchy into a new ActiveRecords such that upon save of the root
object, the root and all it''s children in hierarchy get created as a
set
of new records and the children of the cloned object correctly point to
their parents ?
Is it just a matter of setting the "@new_record" attribute to
"true" for
each of the cloned objects in the hierarchy, after I called the clone
method ?
Thanks,
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
In the end I decided to write a custom routine to clone the hierarchy of ActiveRecords and play it safe; I believe the above approach will work, but since my hiearchy was only 3 levels deep and a few attributes at each level, I played it safe and hand coded it. On May 3, 1:05 am, Edwin Meijer <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I am using the Marshal module to create a deepcloneof an > ActiveRecord object hierarchy... i.e. an object that has_many > children, which at their turn may have many children, etc. > > In model file of the root object I have the following method: > > def deep_clone() > > aClone = Marshal::load(Marshal.dump(self)) > > return aClone > > end > > This works fine, the returnedclonecontains 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... if I were to save it (e.g. > aClone.save), I am effectively updating the same database record as the > original. > > Question: what''s the best way to turn these cloned objects in the > hierarchy into a new ActiveRecords such that upon save of the root > object, the root and all it''s children in hierarchy get created as a set > of new records and the children of the cloned object correctly point to > their parents ? > > Is it just a matter of setting the "@new_record" attribute to "true" for > each of the cloned objects in the hierarchy, after I called theclone > method ? > > Thanks, > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---