Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle dependent destroy via active jobs

I have a couple models with many children. Dependent destroy has gotten really heavy. Anyone know of a way to tie dependent destroy into active jobs? Or, is my only option to remove dependent destroy and role my own jobs via callbacks on the parent model?

like image 919
hellion Avatar asked Mar 21 '15 04:03

hellion


People also ask

How does dependent destroy work?

what is dependent :destroy. Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.

How to delete data from db in Rails?

The standard way to delete associated data in Rails is to let ActiveRecord handle it via dependent: :destroy . In the following example, when the parent model ( author ) is deleted, all data in the dependent models will get deleted by ActiveRecord as well. There is an indexed foreign key, but no foreign key constraint.


2 Answers

Rails does not do this natively. However, this gem does a good job of fixing N+1s related to dependent: :destroy. It actually uses dependent: :delete_all, but uses it in such a way that all sub classes are deleted too. And, it does it using only 2 hits to the DB per class. I can't believe functionality like this isn't wrapped into rails core. https://github.com/jisaacks/recurse-delete

like image 38
hellion Avatar answered Sep 29 '22 18:09

hellion


You can create a worker to destroy the models async and enqueue it's deletion. Something like:

class ComplexModelDeletion < ActiveJob::Base
  def perform(model)
    model.destroy!
  end
end

And the model could be something like:

class Model < ActiveRecord::Base
  def destroy_later
    ComplexModelDeletion.enqueue(self)
  end
end

Then, whenever you need to kill an instance of this model, you could call Model#destroy_later to enqueue it. You could even mark the object as deleted before enqueuing it just to prevent it from being retrieve from DB before it is actually killed.

Not sure if this would work as is, but just to give you an idea of what you could do.

like image 179
lsdr Avatar answered Sep 29 '22 18:09

lsdr