Is there a way to copy the associations of one model to another...
template_model = MyModel.find(id)
new_model = template_model.clone
new_model.children << template_model.children # I want to *copy* children
...such that I copy the children from the template to the new model? (In fact this code moves children from the template to the new model).
I know I can do it manually be looping, but is there are more succinct way?
Thanks
The problem is that you are cloning the template, but not cloning it's children. Try something like:
template_model = MyModel.find(id)
new_model = template_model.clone
new_model.children << template_model.children.collect { |child| child.clone }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With