, Mongoid DocumentNotFound find, ( ). . Mongoid:
, , - , DocumentNotFound ( ), ( ).
:
raise Mongoid::Errors::DocumentNotFound.new(User, params[:name]) unless @current_account.users.first(:conditions => {:name => params[:name]})
: , , (, , - !):
@current_account.users.where!(:conditions => {:name => params[:name]})
Mongoid::CollectionEmpty, , , . , , , , .
, , Mongoid::CollectionEmpty ( ).
module Mongoid
module Criterion
module WithErrors
extend ActiveSupport::Concern
module ClassMethods
def where!(*args)
criteria = self.where(args)
raise Mongoid::EmptyCollection(criteria) if criteria.empty?
criteria
end
end
end
end
class EmptyCollection < StandardError
def initialize(criteria)
@class_name = criteria.class
@selector = criteria.selector
end
def to_s
"Empty collection found for #{@class_name}, using selector: #{@selector}"
end
end
end
module ApplicationName
class Application < Rails::Application
require 'mongoid_criterion_with_errors'
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Criterion::WithErrors
end