Module: Mongoid::Copyable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
lib/mongoid/copyable.rb

Overview

This module contains the behavior of Mongoid’s clone/dup of documents.

Instance Method Summary collapse

Instance Method Details

#cloneDocument Also known as: dup

Clone or dup the current Document. This will return all attributes with the exception of the document’s id, and will reset all the instance variables.

This clone also includes embedded documents. If there is an _id field in the embedded document, it will be maintained, unlike the root’s _id.

If cloning an embedded child, the embedded parent is not cloned and the embedded_in association is not set.

Examples:

Clone the document.

document.clone

Returns:



24
25
26
27
28
29
30
31
# File 'lib/mongoid/copyable.rb', line 24

def clone
  # @note This next line is here to address #2704, even though having an
  # _id and id field in the document would cause problems with Mongoid
  # elsewhere. Note this is only done on the root document as we want
  # to maintain the same _id on the embedded documents.
  attrs = clone_document.except(*self.class.id_fields)
  Copyable.clone_with_hash(self.class, attrs)
end