instance method
update_columns
Ruby on Rails 4.1.16
Since v4.0.13Signature
update_columns(attributes)
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_onare not updated.
This method raises an ActiveRecord::ActiveRecordError when called on new objects, or when at least one of the attributes is marked as readonly.
Parameters
-
attributesreq
Source
# File activerecord/lib/active_record/persistence.rb, line 272
def update_columns(attributes)
raise ActiveRecordError, "cannot update on a new record object" unless persisted?
attributes.each_key do |key|
verify_readonly_attribute(key.to_s)
end
updated_count = self.class.unscoped.where(self.class.primary_key => id).update_all(attributes)
attributes.each do |k, v|
raw_write_attribute(k, v)
end
updated_count == 1
end
Defined in activerecord/lib/active_record/persistence.rb line 272
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Persistence