Method: Mongoid::Persistable::Updatable#update_attribute

Defined in:
lib/mongoid/persistable/updatable.rb

#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



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongoid/persistable/updatable.rb', line 26

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