Module: Mongoid::Reloadable

Included in:
Composable
Defined in:
build/mongoid-8.1/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:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'build/mongoid-8.1/lib/mongoid/reloadable.rb', line 18

def reload
  reloaded = _reload
  if Mongoid.raise_not_found_error && (reloaded.nil? || reloaded.empty?)
    shard_keys = atomic_selector.with_indifferent_access.slice(*shard_key_fields, :_id)
    raise Errors::DocumentNotFound.new(self.class, _id, shard_keys)
  end

  reset_atomic_updates!

  @attributes = reloaded
  @attributes_before_type_cast = @attributes.dup
  @changed_attributes = {}
  @previous_changes = {}
  @previous_attributes = {}
  @previously_new_record = false
  reset_readonly
  apply_defaults
  reload_relations
  run_callbacks(:find) unless _find_callbacks.empty?
  run_callbacks(:initialize) unless _initialize_callbacks.empty?
  self
end