I have a tree of models that represent a book. Looks like this:
book sections documents paragraphs index entries glossary entries footnotes index entries glossary entries
These are all models with has_many from parent and the child has belongs_to. They also all have dependent => :destroy pointing down the tree. Delete a section and it nukes everything beneath it.
There is only one problem.
If you select a document, and delete... the AR query is HUGE as it searches through and finds each paragraph, which then finds each index, glossary, footnote, which then finds each index and glossary.
This takes a LONG time... finding, opening, deleting each record... thousands of database queries.
I need a way to speed this up as it is non functional.
Two ways I can see:
Remove the dependent => destroy and make a before_destroy method which puts the child delete in a transaction, and then cascade that same before_destroy method in each subclass (feels like a bad hack).
Create a stored procedure in Mysql to handle it (not portable).
Has anyone else fixed this problem before? What is the best way to go here?
Regards
Mikel Lindsaar.