instance method
destroy
Ruby on Rails 7.1.6
Since v5.2.8.1 Last seen in v7.1.6Signature
destroy(id)
Destroy an object (or multiple objects) that has the given id. The object is instantiated first, therefore all callbacks and filters are fired off before the object is deleted. This method is less efficient than #delete but allows cleanup methods and other actions to be run.
This essentially finds the object (or multiple objects) with the given id, creates a new object from the attributes, and then calls destroy on it.
Parameters
-
id- This should be the id or an array of ids to be destroyed.
Examples
# Destroy a single object Todo.destroy(1) # Destroy multiple objects todos = [1,2,3] Todo.destroy(todos)
Parameters
-
idreq
Source
# File activerecord/lib/active_record/persistence.rb, line 533
def destroy(id)
multiple_ids = if composite_primary_key?
id.first.is_a?(Array)
else
id.is_a?(Array)
end
if multiple_ids
find(id).each(&:destroy)
else
find(id).destroy
end
end
Defined in activerecord/lib/active_record/persistence.rb line 533
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Persistence::ClassMethods