Module: Mongoid::Persistable::Destroyable

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Persistable
Defined in:
build/mongoid-8.1/lib/mongoid/persistable/destroyable.rb

Overview

Defines behavior for persistence operations that destroy documents.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy(options = nil) ⇒ true | false

Remove the document from the database with callbacks.

Examples:

Destroy a document.

document.destroy

Parameters:

  • options (Hash) (defaults to: nil)

    Options to pass to destroy.

Returns:

  • (true | false)

    True if successful, false if not.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'build/mongoid-8.1/lib/mongoid/persistable/destroyable.rb', line 18

def destroy(options = nil)
  raise Errors::ReadonlyDocument.new(self.class) if readonly?
  self.flagged_for_destroy = true
  result = run_callbacks(:destroy) do
    if catch(:abort) { apply_destroy_dependencies! }
      delete(options || {})
    else
      false
    end
  end
  self.flagged_for_destroy = false
  result
end

#destroy!(options = {}) ⇒ Object



32
33
34
# File 'build/mongoid-8.1/lib/mongoid/persistable/destroyable.rb', line 32

def destroy!(options = {})
  destroy || raise(Errors::DocumentNotDestroyed.new(_id, self.class))
end