instance method
update!
Ruby on Rails 7.1.6
Since v7.0.10Signature
update!(id = :all, attributes)
Updates the object (or multiple objects) just like #update but calls #update! instead of update, so an exception is raised if the record is invalid and saving will fail.
Parameters
-
idopt = :all -
attributesreq
Source
# File activerecord/lib/active_record/persistence.rb, line 434
def update!(id = :all, attributes)
if id.is_a?(Array)
if id.any?(ActiveRecord::Base)
raise ArgumentError,
"You are passing an array of ActiveRecord::Base instances to `update!`. " \
"Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`."
end
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
object.update!(attributes[idx])
}
elsif id == :all
all.each { |record| record.update!(attributes) }
else
if ActiveRecord::Base === id
raise ArgumentError,
"You are passing an instance of ActiveRecord::Base to `update!`. " \
"Please pass the id of the object by calling `.id`."
end
object = find(id)
object.update!(attributes)
object
end
end
Defined in activerecord/lib/active_record/persistence.rb line 434
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Persistence::ClassMethods