If you set the :dependent option to: :destroy, when the object is destroyed, destroy will be called on its associated objects. :delete, when the object is destroyed, all its associated objects will be deleted directly from the database without calling their destroy method.
Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted). Returns the frozen instance.
If you use delete_all , it will skip all the destroy callbacks for the records that you're deleting (helpful for when you don't want these to run) and it will return the number of records that have been deleted.
The difference is with the callback.
The :delete_all
is made directly in your application and deletes by SQL :
DELETE * FROM users where compagny_id = XXXX
With the :destroy
, there is an instantiation of all of your children. So, if you can't destroy it or if each has their own :dependent
, its callbacks can be called.
On a Rails' model association you can specify the :dependent
option, which can take one of the following three forms:
:destroy/:destroy_all
The associated objects are destroyed alongside this object by calling their destroy
method:delete/:delete_all
All associated objects are destroyed immediately without calling their :destroy
method:nullify
All associated objects' foreign keys are set to NULL
without calling their save
callbacksSee destroy deletes its associated elements where delete_all can delete multiple data from self table as DELETE * FROM table where field = 'xyz'
:Dependent possible options:
Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails executes callbacks in order. Therefore, other similar callbacks may affect the :dependent behavior, and the :dependent
behavior may affect other callbacks.
:destroy
causes all the associated objects to also be destroyed.
:delete_all
causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).
:nullify
causes the foreign keys to be set to NULL. Callbacks are not executed.
:restrict_with_exception
causes an exception to be raised if there are any associated records.
:restrict_with_error
causes an error to be added to the owner if there are any associated objects.
If using with the :through
option, the association on the join model must be a belongs_to, and the records which get deleted are the join records, rather than the associated records.
Actually the main difference is that any callbacks will not be invoked when :delete_all
was used. But when used :destroy
the callbacks stack (:after_destroy
, :after_commit
...) will be fired.
Consequently, if you have touch:
ing declarations in models being deleted then it's better to use dependent: :delete_all
rather 'dependent: :destroy'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With