我正在寻找从表中删除记录的最佳方法。例如,我有一个用户,其用户ID横跨多个表。我想删除这个用户和所有表中具有他的ID的每个记录。
u = User.find_by_name('JohnBoy')
u.usage_indexes.destroy_all
u.sources.destroy_all
u.user_stats.destroy_all
u.delete
这可以工作,并从所有表中删除用户的所有引用,但我听说destroy_all是非常繁重的进程,所以我尝试了delete_all。它只是将用户从自己的用户表中删除,并将所有其他表中的id设为空,但保留其中的记录。有人能分享一下执行这样的任务的正确流程吗?
我看到destroy_all对所有关联对象调用destroy函数,但我只想确认正确的方法。
我制作了一个小gem,可以减轻在某些情况下手动删除相关记录的需要。
This gem adds a new option for ActiveRecord associations:
dependent: :delete_recursively
When you destroy a record, all records that are associated using this option will be deleted recursively (i.e. across models), without instantiating any of them.
Note that, just like dependent: :delete or dependent: :delete_all, this new option does not trigger the around/before/after_destroy callbacks of the dependent records.
However, it is possible to have dependent: :destroy associations anywhere within a chain of models that are otherwise associated with dependent: :delete_recursively. The :destroy option will work normally anywhere up or down the line, instantiating and destroying all relevant records and thus also triggering their callbacks.