Rails :dependent => :destroy VS :dependent => :delete_all

Sergey picture Sergey · May 9, 2010 · Viewed 97.9k times · Source

In rails guides it's described like this:

Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :delete_all

Right, cool. But what's the difference between being destroyed and being deleted? I tried both and it seems to do the same thing.

Answer

shingara picture shingara · May 9, 2010

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.