Handling Slow Cascading Deletes In Rails Feb 27, 2020
Daddy Warbucks : You lock the orphans in the closet.
Miss Hannigan : They love it!
If you’re using a development framework to interact with data (Rails, Django, Express, whatever), you’ve most likely had to make smart choices on how cascading deletes work in your system. And often in large systems, you’re forced to make a compromise…
You could have your app and framework manage all those deletes keeping close control over validation and object lifecycles, which is ideal but often poor in performance for large records sets. Or, you could have your database handle the cascading deletes, but then you lose things like your lifecycle hooks.
So today, for Rails, we’re releasing a small but useful utility that provides an alternative (and in some cases, better) way to do cascading deletes/destroys. It’s called Miss Hannigan and you can find it: here.
To quickly catch beginners up, Rails has some great tooling to deal with parent-child relationships using
class Parent < ApplicationRecord has_many :children end
By default, what happens to
children when you delete an instance of Parent? Nothing. Children just sit tight or in our more typical vernacular, they’re orphaned.
To cleanup those orphaned records, you consider two Rails options we’ll explain below: destroy the children, or delete the children. Both options present some problems.Read more…