Module: Mongoid::Reloadable

Included in:
Composable
Defined in:
lib/mongoid/reloadable.rb

Overview

This module handles reloading behavior of documents.

Instance Method Summary collapse

Instance Method Details

#reloadDocument

Reloads the Document attributes from the database. If the document has not been saved then an error will get raised if the configuration option was set. This can reload root documents or embedded documents.

Examples:

Reload the document.

person.reload

Returns:

  • (Document)

    The document, reloaded.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/reloadable.rb', line 16

def reload
  reloaded = _reload
  check_for_deleted_document!(reloaded)

  # In an instance where we create a new document, but set the ID to an existing one,
  #   when the document is reloaded, we want to set new_record to false.
  #   This is necessary otherwise saving will fail, as it will try to insert the document,
  #   instead of attempting to update the existing document.
  @new_record = false unless reloaded.nil? || reloaded.empty?

  reset_object!(reloaded)

  run_callbacks(:find) unless _find_callbacks.empty?
  run_callbacks(:initialize) unless _initialize_callbacks.empty?
  self
end