我有一个包含嵌入文档的mongomapper文档,并希望复制它.
从本质上讲,我想要做的是这样的事情:
customer = Customer.find(params[:id])
new_customer = Customer.new
new_customer = customer
new_customer.save
Run Code Online (Sandbox Code Playgroud)
所以我想最终得到两个不同的mongomapper文档,但内容相同.
有什么想法应该怎么做?
要实现此目的,您需要更改_id. 具有相同内容的文档_id被假定为相同文档,因此保存具有不同内容的文档_id将创建一个新文档。
customer = Customer.find(params[:id])
customer._id = BSON::ObjectId.new # Change _id to make a new record
# NOTE: customer will now persist as a new document like the new_document
# described in the question.
customer.save # Save the new object
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我倾向于将旧记录存储_id在新记录中的某个位置,这样我就可以跟踪谁派生于谁,但这不是必要的。
| 归档时间: |
|
| 查看次数: |
2443 次 |
| 最近记录: |