Rails :dependent => :destroy VS :dependent => :delete_all
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 theirdestroy
method:delete/:delete_all
All associated objects are destroyed immediately without calling their:destroy
method:nullify
All associated objects' foreign keys are set toNULL
without calling theirsave
callbacks
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.