ActiveRecord safely supports dup, but mongoid doesn't appear to handle it properly.
I'd like to do the following:
x = MyModel.new
x.save
y = x.dup
y.save
And y should be a totally new object such that:
x != y
x.id != y.id
Try this:
x = Item.new
x.save
y = x.clone
y.save
It should change the _id and copy all the other fields. I've noticed this doesn't seem to work with embedded documents though. For each embedded doc in the original, it creates a blank embedded doc in the clone with a new id, but doesn't populate any of the other fields.
If working with embedded docs it might be better to write your own class method.
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