instance method update!

Ruby on Rails 7.2.3

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

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

id opt = :all
attributes req
Source
# File activerecord/lib/active_record/persistence.rb, line 158
      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 158 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Persistence::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close