Have you got a / can you help with a generic method for doing a deep copy of a set of Rails objects? I see there is a clone method for individual objects. I can also see it would be pretty easy to implement a method that copies down a tree. The tricky bit is how to deep copy where objects within the tree reference each other? Thanks for any tips. Tom ---- Tom Counsell. http://tom.counsell.org
On 03 Jan 2005, at 12:44, Thomas Counsell wrote:> Have you got a / can you help with a generic method for doing a deep > copy of a set of Rails objects? > > I see there is a clone method for individual objects. > > I can also see it would be pretty easy to implement a method that > copies down a tree. > > The tricky bit is how to deep copy where objects within the tree > reference each other? > > Thanks for any tips.class Object def deep_clone Marshal.load Marshal.dump(self) end end -- Eric Hodel - drbrain-48TerJ1FxhPk1uMJSBkQmQ@public.gmane.org - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 03 Jan 2005, at 12:44, Thomas Counsell wrote:> Have you got a / can you help with a generic method for doing a deep > copy of a set of Rails objects? > > I see there is a clone method for individual objects. > > I can also see it would be pretty easy to implement a method that > copies down a tree. > > The tricky bit is how to deep copy where objects within the tree > reference each other? > > Thanks for any tips.class Object def deep_clone Marshal.load Marshal.dump(self) end end -- Eric Hodel - drbrain-48TerJ1FxhPk1uMJSBkQmQ@public.gmane.org - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sorry, just to clarify, I guess I''m really trying to duplicate a set of rows in various database tables, to which the Rails ActiveRecord objects point. I suspect (will check later) that Marshal.load Marshal.dump( self ) will just create a duplicate set of objects without duplicating the underlying table rows? or are Active Record objects cleverer than that? Apologies for not being clear in my original post. ---- Tom Counsell. http://tom.counsell.org On 4 Jan 2005, at 00:20, Eric Hodel wrote:> On 03 Jan 2005, at 12:44, Thomas Counsell wrote: > >> Have you got a / can you help with a generic method for doing a deep >> copy of a set of Rails objects? >> >> I see there is a clone method for individual objects. >> >> I can also see it would be pretty easy to implement a method that >> copies down a tree. >> >> The tricky bit is how to deep copy where objects within the tree >> reference each other? >> >> Thanks for any tips. > > class Object > def deep_clone > Marshal.load Marshal.dump(self) > end > end
try @model.dup.save or Model.create( @model.attributes ) On Tue, 4 Jan 2005 08:59:29 +0000, Thomas Counsell <tamc2-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org> wrote:> Sorry, just to clarify, > > I guess I''m really trying to duplicate a set of rows in various > database tables, to which the Rails ActiveRecord objects point. > > I suspect (will check later) that Marshal.load Marshal.dump( self ) > will just create a duplicate set of objects without duplicating the > underlying table rows? or are Active Record objects cleverer than that? > > Apologies for not being clear in my original post. > > ---- > Tom Counsell. http://tom.counsell.org > > On 4 Jan 2005, at 00:20, Eric Hodel wrote: > > > On 03 Jan 2005, at 12:44, Thomas Counsell wrote: > > > >> Have you got a / can you help with a generic method for doing a deep > >> copy of a set of Rails objects? > >> > >> I see there is a clone method for individual objects. > >> > >> I can also see it would be pretty easy to implement a method that > >> copies down a tree. > >> > >> The tricky bit is how to deep copy where objects within the tree > >> reference each other? > >> > >> Thanks for any tips. > > > > class Object > > def deep_clone > > Marshal.load Marshal.dump(self) > > end > > end > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
Thanks Tobias. I can see that would duplicate a single object / table row, but I''d also like to have all the objects that the object refers to duplicated as well (those defined in the has_many: macros). Any idea how best to do that? Thanks. Tom On 4 Jan 2005, at 09:03, Tobias Luetke wrote:> try @model.dup.save or Model.create( @model.attributes ) > > > On Tue, 4 Jan 2005 08:59:29 +0000, Thomas Counsell <tamc2-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org> > wrote: >> Sorry, just to clarify, >> >> I guess I''m really trying to duplicate a set of rows in various >> database tables, to which the Rails ActiveRecord objects point. >> >> I suspect (will check later) that Marshal.load Marshal.dump( self ) >> will just create a duplicate set of objects without duplicating the >> underlying table rows? or are Active Record objects cleverer than >> that? >> >> Apologies for not being clear in my original post. >> >> ---- >> Tom Counsell. http://tom.counsell.org >> >> On 4 Jan 2005, at 00:20, Eric Hodel wrote: >> >>> On 03 Jan 2005, at 12:44, Thomas Counsell wrote: >>> >>>> Have you got a / can you help with a generic method for doing a deep >>>> copy of a set of Rails objects? >>>> >>>> I see there is a clone method for individual objects. >>>> >>>> I can also see it would be pretty easy to implement a method that >>>> copies down a tree. >>>> >>>> The tricky bit is how to deep copy where objects within the tree >>>> reference each other? >>>> >>>> Thanks for any tips. >>> >>> class Object >>> def deep_clone >>> Marshal.load Marshal.dump(self) >>> end >>> end >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Tobi > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails