[Rails] Deep Copy Method?

Thomas Counsell tamc2 at cam.ac.uk
Tue Jan 4 10:14:35 GMT 2005


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 at cam.ac.uk> 
> 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 at lists.rubyonrails.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 at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails



More information about the Rails mailing list