07-18-2023, 01:43 PM
I am looking for the best approach to delete records from a table. For instance, I have a user whose user ID is across many tables. I want to delete this user and every record that has his ID in all tables.
u = User.find_by_name('JohnBoy')
u.usage_indexes.destroy_all
u.sources.destroy_all
u.user_stats.destroy_all
u.delete
This works and removes all references of the user from all tables, but I heard that `destroy_all` was very process heavy, so I tried `delete_all`. It only removes the user from his own user table and the `id` from all the other tables are made null, but leaves the records intact in them. Can someone share what the correct process is for performing a task like this?
I see that `destroy_all` calls the `destroy` function on all associated objects but I just want to confirm the correct approach.
u = User.find_by_name('JohnBoy')
u.usage_indexes.destroy_all
u.sources.destroy_all
u.user_stats.destroy_all
u.delete
This works and removes all references of the user from all tables, but I heard that `destroy_all` was very process heavy, so I tried `delete_all`. It only removes the user from his own user table and the `id` from all the other tables are made null, but leaves the records intact in them. Can someone share what the correct process is for performing a task like this?
I see that `destroy_all` calls the `destroy` function on all associated objects but I just want to confirm the correct approach.