Search code examples
ruby-on-railsmodels

model relations and bulk deletes


I have a model relation of dependant=>destroy that has to do 50K+ deletes when the destroy is triggered. Looking at the console, rails is trying to do an explicit delete with ID for every single row, which is taking a while. Is there a way for me to force rails to do a bulk delete? Or, I can remove the model dependency, is there a way to do this kind of bulk delete from the code?

Thanks


Solution

  • You should be able to set dependent: delete_all

    If you can't get that to work, you might want to use delete_all in your own callback.

    To be clear, delete_all should generate a single statement to delete all child objects