Module: Mongoid::Persistable::Savable

Included in:
Mongoid::Persistable
Defined in:
lib/mongoid/persistable/savable.rb

Overview

Defines behaviour for persistence operations that save documents.

Since:

  • 4.0.0

Instance Method Summary collapse

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.

Returns:

  • (true, false)

    True is success, false if not.

Since:

  • 1.0.0



21
22
23
24
25
26
27
# File 'lib/mongoid/persistable/savable.rb', line 21

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.

Returns:

  • (true, false)

    True if validation passed.

Raises:

Since:

  • 1.0.0



43
44
45
46
47
48
49
# File 'lib/mongoid/persistable/savable.rb', line 43

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