# Duplicate model object with associations?

**URL:** <https://www.ruby-forum.com/t/duplicate-model-object-with-associations/173282>\
**Category:** Rails\
**Created:** [August 6, 2009, 7:03am UTC](https://www.ruby-forum.com/t/duplicate-model-object-with-associations/173282 "2009-08-06T07:03:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [August 6, 2009, 7:03am UTC](https://www.ruby-forum.com/t/duplicate-model-object-with-associations/173282/1 "2009-08-06T07:03:18Z")

</div>

## class Patient \< AR::Base has\_many :aaas has\_many :bbbs ,:through=\>:aaas end class Aaa \< AR::Base has\_many :bbbs end

i want like this  
@patient=Patient.find(id)  
@new\_patient=@patient.duplicate

now the  
@new\_patient should have their own copy of patient,aaas,bbbs (new  
records in corresponding tables)

there is one options is that  
we can create seperate object for each patient,aaas(Array),bbbs(Array)  
and save them, but if the associations(aaas,bbbs) size(Array.size) are  
more then it will take too much time

i know RAILS framework don’t have any support for this

any short ideas or ready mate plugins ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [August 6, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/duplicate-model-object-with-associations/173282/2 "2009-08-06T19:29:42Z")

</div>

Sniper A. wrote:

> ## class Patient \< AR::Base has\_many :aaas has\_many :bbbs ,:through=\>:aaas end class Aaa \< AR::Base has\_many :bbbs end
> 
> i want like this  
> @patient=Patient.find(id)  
> @new\_patient=@patient.duplicate
> 
> now the  
> @new\_patient should have their own copy of patient,aaas,bbbs (new  
> records in corresponding tables)

When you said it took too much time, did you test it? Secondly, rails  
does support this directly through active record…

new\_patient = Patient.new(:aaas =\> @patient.aaas).save!

If the aaas and the bbbs have to be deep copied as well, then you can  
provide your own dup method for them.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [August 6, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/duplicate-model-object-with-associations/173282/3 "2009-08-06T19:59:48Z")

</div>

On Aug 6, 1:03 am, Sniper A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @removed\_email\_address@domain.invalid  
> i know RAILS framework don’t have any support for this
> 
> any short ideas or ready mate plugins ?

No, but if you find one, the computer science community will be  
excited. You seem to be asking for a way to copy an array that takes  
less time than copying the elements.

Worrying about speed for something like this is vastly premature  
optimization…

–Matt J.
