Forum: Ruby on Rails Cloning ActiveRecord objects

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.
Rob L. (Guest)
on 2009-01-06 13:45
(Received via mailing list)
Hi there,

I've been working on a solution to try and fix a problem I am getting
with the Interlock plugin. However, my quick solution was to clone my
ActiveRecord object and store the clone. I now realise that cloning
ActiveRecord objects merely extracts the attributes and creates a new
object with the same attributes. What I want is to clone the whole
object, in this case assocations aren't necessary just the object with
the correct id.

Is there any reason why this would be bad? I guess cloning the object is
generally used to duplicate records but here I want to take the object
and cache it without affecting the original object.

Harold (Guest)
on 2009-01-06 15:47
(Received via mailing list)
Try your_active_record_instance.dup instead of .clone.
This topic is locked and can not be replied to.