- If you use
:dependent => :destroy
in your Rails model, each model to be destroyed will be instantiated and the destroy()
method will be called upon it. This will result in the normal object destruction lifecycle, include any callbacks. Destroying children and grandchildren can be handled gracefully. It is a safe, normal way to destroy objects. If you’re not sure whether to use :dependent => :destroy
or :dependent => :delete_all
, use :dependent => destroy
. - If you use
:dependent => :delete_all
in your Rails model, no objects will be loaded into memory and no destroy()
methods will be called. Instead, a simple SQL query along the lines of DELETE * FROM dependent_model where depending_model_id = N
will be run. There is no concern paid to destroying children and grandchildren; they will be orphaned in the database. If constructing a dependend model object and invoking destroy()
is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.
Posted on Dec 20, 2014 to Ruby on Rails