123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Destroyable

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/persistable/destroyable.rb

Overview

Defines behavior for persistence operations that destroy documents.

Instance Method Summary

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)

    The options.

Options Hash (options):

  • :persist (true | false)

    Whether to persist the delete action. Callbacks will still be run even if false.

  • :suppress (true | false)

    Whether to update the parent document in-memory when deleting an embedded document.

Returns:

  • (true | false)

    True if successful, false if not.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/persistable/destroyable.rb', line 23

def destroy(options = nil)
  raise Errors::ReadonlyDocument.new(self.class) if readonly?
  self.flagged_for_destroy = true
  result = run_callbacks(:commit, skip_if: -> { in_transaction? }) do
    run_callbacks(:destroy) do
      if catch(:abort) { apply_destroy_dependencies! }
        delete(options || {}).tap do |res|
          if res && in_transaction?
            Threaded.add_modified_document(_session, self)
          end
        end
      else
        false
      end
    end
  end
  self.flagged_for_destroy = false
  result
end

#destroy!(options = {}) ⇒ true

Remove the document from the database with callbacks. Raises an error if the document is not destroyed.

Examples:

Destroy a document.

document.destroy!

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :persist (true | false)

    Whether to persist the delete action. Callbacks will still be run even if false.

  • :suppress (true | false)

    Whether to update the parent document in-memory when deleting an embedded document.

Returns:

  • (true)

    Always true.

[ GitHub ]

  
# File 'lib/mongoid/persistable/destroyable.rb', line 59

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