Forum: Ruby on Rails AR deep clone

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael R. (Guest)
on 2009-01-06 20:25
Hi all,

I'm having problems with cloning an ActiveRecord Object. The object
consists of multiple child objects, so when I use the normal clone
method, it only clones the attributes of the parent object.

Could someone help me out telling me how I can clone the child objects
as wel? I have tried like:

      tmp.my_items.each do |item|
        @clone.my_items.build(item.clone)
      end

but that didn't help. I know it must be something fairly easy, but I
can't figure it out.

Thank you in advance for the help.
Harold (Guest)
on 2009-01-07 00:05
(Received via mailing list)
Ran into this a while back.

your_ar_instance.dup will keep the child records as well.

On Jan 6, 1:25 pm, Michael R. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.