Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a deep copy of a record?

I need to be able to copy a record. By that I mean that: All the references need to be deep copied.

For example, if I have a Case and it has a patient_id linking to a patient, to copy a case would mean to create a copy of the patient too.

Therefore, what would be a 'best practices' way to do this. My first thought was to make a create_copy method for all my models and each of these calls that for all their dependencies to create a cascading deep copy.

I am just wondering if there is a more 'rails' way to do this where I could add on to the clone function?

like image 360
jmasterx Avatar asked Feb 01 '26 18:02

jmasterx


1 Answers

You may want to give the Amoeba gem a try.

Reference: https://stackoverflow.com/a/9485672/336920

Also deep_clonable.

They both work with Rails 4 and have been updated recently.

like image 161
B Seven Avatar answered Feb 04 '26 10:02

B Seven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!