Exception: Mongoid::Errors::DocumentNotFound

Inherits:
MongoidError
  • Object
show all
Defined in:
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 | Hash) (defaults to: nil)

    The unmatched ids, if appropriate. If there is a shard key this will be a hash.



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

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

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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File '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 'lib/mongoid/errors/document_not_found.rb', line 12

def params
  @params
end