Exception: Mongoid::Errors::DocumentNotFound

Inherits:
MongoidError
  • Object
show all
Defined in:
build/mongoid-7.3/lib/mongoid/errors/document_not_found.rb

Overview

Raised when querying the database for a document by a specific id or by set of attributes which does not exist. If multiple ids were passed then it will display all of those.

Constant Summary

Constants inherited from MongoidError

MongoidError::BASE_KEY

Instance Attribute Summary collapse

Attributes inherited from MongoidError

#problem, #resolution, #summary

Instance Method Summary collapse

Methods inherited from MongoidError

#compose_message

Constructor Details

#initialize(klass, params, unmatched = nil) ⇒ DocumentNotFound

Create the new error.

Examples:

Create the error.

DocumentNotFound.new(Person, ["1", "2"])

Create the error with attributes instead of ids

DocumentNotFound.new(Person, :ssn => "1234", :name => "Helen")

Parameters:

  • klass (Class)

    The model class.

  • params (Hash, Array, Object)

    The attributes or ids.

  • unmatched (Array) (defaults to: nil)

    The unmatched ids, if appropriate



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'build/mongoid-7.3/lib/mongoid/errors/document_not_found.rb', line 25

def initialize(klass, params, unmatched = nil)
  if !unmatched && !params.is_a?(Hash)
    unmatched = Array(params)
  end

  @klass, @params = klass, params
  super(
    compose_message(
      message_key(params),
      {
        klass: klass.name,
        searched: searched(params),
        attributes: params,
        total: total(params),
        missing: missing(unmatched)
      }
    )
  )
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'build/mongoid-7.3/lib/mongoid/errors/document_not_found.rb', line 12

def klass
  @klass
end

#paramsObject (readonly)

Returns the value of attribute params.



12
13
14
# File 'build/mongoid-7.3/lib/mongoid/errors/document_not_found.rb', line 12

def params
  @params
end