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

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



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

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.



10
11
12
# File 'lib/mongoid/errors/document_not_found.rb', line 10

def klass
  @klass
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/mongoid/errors/document_not_found.rb', line 10

def params
  @params
end