Module: Mongoid::Persistable::Updatable

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

Overview

Defines behaviour for persistence operations that update existing documents.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#update(attributes = {}) ⇒ true, false Also known as: update_attributes

Update the document attributes in the database.

Examples:

Update the document’s attributes

document.update(:title => "Sir")

Parameters:

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

    The attributes to update.

Returns:

  • (true, false)

    True if validation passed, false if not.

Since:

  • 1.0.0



76
77
78
79
# File 'lib/mongoid/persistable/updatable.rb', line 76

def update(attributes = {})
  assign_attributes(attributes)
  save
end

#update!(attributes = {}) ⇒ true, false Also known as: update_attributes!

Update the document attributes in the database and raise an error if validation failed.

Examples:

Update the document’s attributes.

document.update!(:title => "Sir")

Parameters:

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

    The attributes to update.

Returns:

  • (true, false)

    True if validation passed.

Raises:

  • (Errors::Validations)

    If validation failed.

  • (Errors::Callbacks)

    If a callback returns false.

Since:

  • 1.0.0



96
97
98
99
100
101
102
103
# File 'lib/mongoid/persistable/updatable.rb', line 96

def update!(attributes = {})
  result = update_attributes(attributes)
  unless result
    fail_due_to_validation! unless errors.empty?
    fail_due_to_callback!(:update_attributes!)
  end
  result
end

#update_attribute(name, value) ⇒ true, false

Update a single attribute and persist the entire document. This skips validation but fires the callbacks.

Examples:

Update the attribute.

person.update_attribute(:title, "Sir")

Parameters:

  • name (Symbol, String)

    The name of the attribute.

  • value (Object)

    The new value of the attribute.a

Returns:

  • (true, false)

    True if save was successfull, false if not.

Raises:

Since:

  • 2.0.0



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/persistable/updatable.rb', line 52

def update_attribute(name, value)
  normalized = name.to_s
  unless attribute_writable?(normalized)
    raise Errors::ReadonlyAttribute.new(normalized, value)
  end
  setter = "#{normalized}="
  if respond_to?(setter)
    send(setter, value)
  else
    write_attribute(database_field_name(normalized), value)
  end
  save(validate: false)
end

#update_document(options = {}) ⇒ true, false

Update the document in the database.

Examples:

Update an existing document.

document.update

Parameters:

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

    Options to pass to update.

Options Hash (options):

  • :validate (true, false)

    Whether or not to validate.

Returns:

  • (true, false)

    True if succeeded, false if not.

Since:

  • 1.0.0



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/persistable/updatable.rb', line 23

def update_document(options = {})
  prepare_update(options) do
    updates, conflicts = init_atomic_updates
    unless updates.empty?
      coll = _root.collection
      selector = atomic_selector
      coll.find(selector).update(positionally(selector, updates))
      conflicts.each_pair do |key, value|
        coll.find(selector).update(positionally(selector, { key => value }))
      end
    end
  end
end