Rails: update_attribute vs update_attributes

2019-01-03 11:36发布

Object.update_attribute(:only_one_field, "Some Value")
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")

Both of these will update an object without having to explicitly tell AR to update.

Rails API says:

for update_attribute

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.

for update_attributes

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

So if I don't want to have the object validated I should use update_attribute. What if I have this update on a before_save, will it stackoverflow?

My question is does update_attribute also bypass the before save or just the validation.

Also, what is the correct syntax to pass a hash to update_attributes... check out my example at the top.

11条回答
小情绪 Triste *
2楼-- · 2019-01-03 12:03

To answer your question, update_attribute skips pre save "validations" but it still runs any other callbacks like after_save etc. So if you really want to "just update the column and skip any AR cruft" then you need to use (apparently)

Model.update_all(...) see https://stackoverflow.com/a/7243777/32453

查看更多
Lonely孤独者°
3楼-- · 2019-01-03 12:08

Also worth noting is that with update_attribute, the desired attribute to be updated doesn't need to be white listed with attr_accessible to update it as opposed to the mass assignment method update_attributes which will only update attr_accessible specified attributes.

查看更多
你好瞎i
4楼-- · 2019-01-03 12:08

Great answers. notice that as for ruby 1.9 and above you could (and i think should) use the new hash syntax for update_attributes:

Model.update_attributes(column1: "data", column2: "data")
查看更多
贪生不怕死
5楼-- · 2019-01-03 12:08

You might be interested in visiting this blog post concerning all the possible ways to assign an attribute or update record (updated to Rails 4) update_attribute, update, update_column, update_columns etc. http://www.davidverhasselt.com/set-attributes-in-activerecord/. For example it differs in aspects such as running validations, touching object's updated_at or triggering callbacks.

As an answer to the OP's question update_attribute does not by pass callbacks.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-03 12:09

update_attribute simply updates only one attribute of a model, but we can pass multiple attributes in update_attributes method.

Example:

user = User.last

#update_attribute
user.update_attribute(:status, "active")

It pass the validation

#update_attributes
user.update_attributes(first_name: 'update name', status: "active")

it doesn't update if validation fails.

查看更多
祖国的老花朵
7楼-- · 2019-01-03 12:11

Tip: update_attribute is being deprecated in Rails 4 via Commit a7f4b0a1. It removes update_attribute in favor of update_column.

查看更多
登录 后发表回答