class Mongoid::Errors::DocumentNotFound

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.

Attributes

klass[R]
params[R]

Public Class Methods

new(klass, params, unmatched = nil) click to toggle source

Create the new error.

@example Create the error.

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

@example Create the error with attributes instead of ids

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

@param [ Class ] klass The model class. @param [ Hash, Array, Object ] params The attributes or ids. @param [ Array ] unmatched The unmatched ids, if appropriate

Calls superclass method
# File 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

Private Instance Methods

message_key(params) click to toggle source

Create the problem.

@example Create the problem.

error.problem

@return [ String ] The problem.

@since 3.0.0

# File lib/mongoid/errors/document_not_found.rb, line 105
def message_key(params)
  case params
    when Hash then "document_with_attributes_not_found"
    else "document_not_found"
  end
end
missing(unmatched) click to toggle source

Get the string to display the document params that were unmatched.

@example Get the missing string.

error.missing(1)

@param [ Object, Array ] unmatched The ids that did not match.

@return [ String ] The missing string.

@since 3.0.0

# File lib/mongoid/errors/document_not_found.rb, line 57
def missing(unmatched)
  if unmatched.is_a?(::Array)
    unmatched.join(", ")
  else
    unmatched
  end
end
searched(params) click to toggle source

Get the string to display the document params that were searched for.

@example Get the searched string.

error.searched(1)

@param [ Object, Array ] params The ids that were searched for.

@return [ String ] The searched string.

@since 3.0.0

# File lib/mongoid/errors/document_not_found.rb, line 75
def searched(params)
  if params.is_a?(::Array)
    params.take(3).join(", ") + " ..."
  else
    params
  end
end
total(params) click to toggle source

Get the total number of expected documents.

@example Get the total.

error.total([ 1, 2, 3 ])

@param [ Object, Array ] params What was searched for.

@return [ Integer ] The total number.

@since 3.0.0

# File lib/mongoid/errors/document_not_found.rb, line 93
def total(params)
  params.is_a?(::Array) ? params.count : 1
end