update_columns
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (9)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (38)
- What's this?
update_columns(attributes)
public
Updates the attributes directly in the database issuing an UPDATE SQL statement and sets them in the receiver:
user.update_columns(last_request_at: Time.current)
This is the fastest way to update attributes because it goes straight to the database, but take into account that in consequence the regular update procedures are totally bypassed. In particular:
-
Validations are skipped.
-
Callbacks are skipped.
-
updated_at/updated_on are updated if the touch option is set to true.
-
However, attributes are serialized with the same rules as ActiveRecord::Relation#update_all
This method raises an ActiveRecord::ActiveRecordError when called on new objects, or when at least one of the attributes is marked as readonly.
Parameters
-
:touch option - Touch the timestamp columns when updating.
-
If attribute names are passed, they are updated along with updated_at/updated_on attributes.
Examples
# Update a single attribute. user.update_columns(last_request_at: Time.current) # Update with touch option. user.update_columns(last_request_at: Time.current, touch: true)

