Reputation: 35400
In Rails I have always used dependent: :destroy
to remove dependent objects.
However in the following scenario it doesn't work properly because Subscriptions and Notifications are very large, with millions of objects that must be destroyed.
Users --> Projects --> Subscriptions
\
--> Notifications
Some problems are:
dependent: :delete_all
on large tables because it would block the database for a long timeuser.destroy
wraps all the deletions inside a transaction (I don't think it's good for a database which must serve other queries in the meantime)What is the Rails way to handle a large destroy with millions of records? Are there any gems that can help and handle the dependent: :destroy
in a better way (e.g. outside a transaction)? What approach would you suggest in this case?
Upvotes: 1
Views: 557
Reputation: 8402
Starting from Rails 6.1 it's possible to do dependent: :destroy_async
to destroy associations in the background
has_many :subscriptions, dependent: :destroy_async
just posted this for future reference
Upvotes: 3