123456789_123456789_123456789_123456789_123456789_

Exception: Mongoid::Errors::DocumentNotFound

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, MongoidError, StandardError
Instance Chain:
self, MongoidError, StandardError
Inherits: Mongoid::Errors::MongoidError
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

MongoidError - Inherited

BASE_KEY

Class Method Summary

Instance Attribute Summary

MongoidError - Inherited

Instance Method Summary

MongoidError - Inherited

#compose_message

Compose the message.

#translate

Given the key of the specific error and the options hash, translate the message.

#translate_problem

Create the problem.

#translate_resolution

Create the resolution.

#translate_summary

Create the summary.

Constructor Details

.new(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.

[ GitHub ]

  
# 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

#klass (readonly)

[ GitHub ]

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

attr_reader :klass, :params

#params (readonly)

[ GitHub ]

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

attr_reader :klass, :params

Instance Method Details

#message_key(params, unmatched) ⇒ String (private)

Create the problem.

Examples:

Create the problem.

error.problem

Returns:

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 101

def message_key(params, unmatched)
  if !params && !unmatched
    "no_documents_found"
  elsif Hash === params
    "document_with_attributes_not_found"
  elsif Hash === unmatched && unmatched.size >= 2
    "document_with_shard_key_not_found"
  else
    "document_not_found"
  end
end

#missing(unmatched) ⇒ String (private)

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

Examples:

Get the missing string.

error.missing(1)

Parameters:

  • unmatched (Object | Array)

    The ids that did not match.

Returns:

  • (String)

    The missing string.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 57

def missing(unmatched)
  if unmatched.is_a?(::Array)
    unmatched.join(", ")
  elsif unmatched.is_a?(::Hash)
    unmatched[:_id] || unmatched["_id"]
  else
    unmatched
  end
end

#searched(params) ⇒ String (private)

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

Examples:

Get the searched string.

error.searched(1)

Parameters:

  • params (Object | Array)

    The ids that were searched for.

Returns:

  • (String)

    The searched string.

[ GitHub ]

  
# 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

#shard_key(unmatched) ⇒ String (private)

Get the shard key from the unmatched hash.

Returns:

  • (String)

    the shard key and value.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 116

def shard_key(unmatched)
  if Hash === unmatched
    h = unmatched.dup
    h.delete("_id")
    h.delete(:_id)
    h.map{|k,v| "#{k}: #{v}" }.join(", ")
  end
end

#total(params) ⇒ Integer (private)

Get the total number of expected documents.

Examples:

Get the total.

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

Parameters:

  • params (Object | Array)

    What was searched for.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 91

def total(params)
  params.is_a?(::Array) ? params.count : 1
end