instance method
destroy_all
Ruby on Rails 5.0.7.2
Since v3.0.20Signature
destroy_all(conditions = nil)
Destroys the records by instantiating each record and calling its #destroy method. Each object’s callbacks are executed (including :dependent association options). Returns the collection of objects that were destroyed; each will be frozen, to reflect that no changes should be made (since they can’t be persisted).
Note: Instantiation, callback execution, and deletion of each record can be time consuming when you’re removing many records at once. It generates at least one SQL DELETE query per record (or possibly more, to enforce your callbacks). If you want to delete many rows quickly, without concern for their associations or callbacks, use #delete_all instead.
Examples
Person.where(age: 0..18).destroy_all
Parameters
-
conditionsopt = nil
Source
# File activerecord/lib/active_record/relation.rb, line 459
def destroy_all(conditions = nil)
if conditions
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
Passing conditions to destroy_all is deprecated and will be removed in Rails 5.1.
To achieve the same use where(conditions).destroy_all.
MESSAGE
where(conditions).destroy_all
else
records.each(&:destroy).tap { reset }
end
end
Defined in activerecord/lib/active_record/relation.rb line 459
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Relation