Module: DeletedAt::ActiveRecord::Relation
- Defined in:
- lib/deleted_at/active_record/relation.rb
Overview
Active Record Relation
Instance Method Summary collapse
-
#delete(id_or_array) ⇒ Object
Deletes the row with a primary key matching the
idargument, using a SQLDELETEstatement, and returns the number of rows deleted. -
#delete_all(conditions = nil) ⇒ Object
Deletes the records matching
conditionswithout instantiating the records first, and hence not calling thedestroymethod nor invoking callbacks. - #deleted_at_attributes ⇒ Object
-
#destroy(id) ⇒ Object
Destroy an object (or multiple objects) that has the given id.
Instance Method Details
#delete(id_or_array) ⇒ Object
Deletes the row with a primary key matching the id argument, using a SQL DELETE statement, and returns the number of rows deleted. Active Record objects are not instantiated, so the object’s callbacks are not executed, including any :dependent association options.
You can delete multiple rows at once by passing an Array of ids.
Note: Although it is often much faster than the alternative, #destroy, skipping callbacks might bypass business logic in your application that ensures referential integrity or performs other essential jobs.
Examples
# Delete a single row
Todo.delete(1)
# Delete multiple rows
Todo.delete([2,3,4])
58 59 60 |
# File 'lib/deleted_at/active_record/relation.rb', line 58 def delete(id_or_array) where(primary_key => id_or_array).delete_all end |
#delete_all(conditions = nil) ⇒ Object
Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks. This is a single SQL DELETE statement that goes straight to the database, much more efficient than destroy_all. Be careful with relations though, in particular :dependent rules defined on associations are not honored. Returns the number of rows affected.
Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")
Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])
Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent associations or call your before_* or after_destroy callbacks, use the destroy_all method instead.
If an invalid method is supplied, delete_all raises an ActiveRecord error:
Post.limit(100).delete_all
# => ActiveRecord::ActiveRecordError: delete_all doesn't support limit
31 32 33 34 35 36 37 |
# File 'lib/deleted_at/active_record/relation.rb', line 31 def delete_all(conditions = nil) if archive_with_deleted_at? where(conditions).update_all(deleted_at_attributes) else super end end |
#deleted_at_attributes ⇒ Object
7 8 9 10 |
# File 'lib/deleted_at/active_record/relation.rb', line 7 def deleted_at_attributes # We _do_ have klass at this point { klass.deleted_at_column => Time.now.utc } end |
#destroy(id) ⇒ Object
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 ActiveRecord#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- Can be either an Integer or an Array of Integers.
Examples
# Destroy a single object
Todo.destroy(1)
# Destroy multiple objects
todos = [1,2,3]
Todo.destroy(todos)
81 82 83 84 85 86 87 |
# File 'lib/deleted_at/active_record/relation.rb', line 81 def destroy(id) if id.is_a?(Array) id.map { |one_id| destroy(one_id) } else find(id).destroy end end |