method

update_attribute

rails latest stable - Class: ActiveRecord::Base

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

update_attribute(name, value)
public

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

5Notes

Validations

Ariejan · Aug 11, 20088 thanks

+update_attribute+ will not perform validations checks when the Validation module is included.

If you want to perform validations when updating, use +update_attributes+ instead.

This method will still rewrite all the values of the table

railsmonk · Feb 3, 20094 thanks

Even if you update only a small boolean flag on your record, update_attribute will generate an UPDATE statement that will include all the fields of the record, including huge BLOB and TEXT columns. Take this in account.

Update statement won't include all attributes with ActiveRecord::Dirty

TheDeadSerious · Oct 22, 20094 thanks

With the addition of ActiveRecord::Dirty, the update statement will only feature changed columns, as opposed to the comment of railsmonk below.

Careful with this method.

arronwashington · Apr 3, 20103 thanks

Despite the name and description, it will actually update any changed fields on the model rather than just the desired attribute.

def update_attribute(name, value)
send(name.to_s + '=', value)
save(false)
end

See? Use update_all and pass in the model ID as a condition, instead.

callbacks

rogerdpack · Aug 19, 2014

As a note, AFAICT, this skips "validations" but does still run all callbacks still [like after_save, etc.] So if you're looking for something that just updates see here: http://stackoverflow.com/a/7243777/32453