Cloning ActiveRecord objects

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.


Try your_active_record_instance.dup instead of .clone.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs