123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Savable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/persistable/savable.rb

Overview

Defines behavior for persistence operations that save documents.

Instance Method Summary

Instance Method Details

#save(options = {}) ⇒ true | false

Save the document - will perform an insert if the document is new, and update if not.

Examples:

Save the document.

document.save

Parameters:

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

    Options to pass to the save.

Options Hash (options):

  • :touch (true | false)

    Whether or not the updated_at attribute will be updated with the current time. When this option is false, none of the embedded documents will be touched. This option is ignored when saving a new document, and the created_at and updated_at will be set to the current time.

Returns:

  • (true | false)

    True if success, false if not.

[ GitHub ]

  
# File 'lib/mongoid/persistable/savable.rb', line 25

def save(options = {})
  if new_record?
    !insert(options).new_record?
  else
    update_document(options)
  end
end

#save!(options = {}) ⇒ true | false

Save the document - will perform an insert if the document is new, and update if not. If a validation error occurs an error will get raised.

Examples:

Save the document.

document.save!

Parameters:

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

    Options to pass to the save.

Options Hash (options):

  • :touch (true | false)

    Whether or not the updated_at attribute will be updated with the current time. When this option is false, none of the embedded documents will be touched.This option is ignored when saving a new document, and the created_at and updated_at will be set to the current time.

Returns:

  • (true | false)

    True if validation passed.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/persistable/savable.rb', line 51

def save!(options = {})
  unless save(options)
    fail_due_to_validation! unless errors.empty?
    fail_due_to_callback!(:save!)
  end
  true
end