DELETE_ALL VS destroy_all?DELETE_ALL、VS、destroy_all

2023-09-08 18:52:21 作者:想吃糖糖

我要寻找从表中删除记录的最佳方法。举例来说,我有一个用户,其用户ID在许多表。我想删除这个用户,并且有他的所有表中标识每个记录。

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

该作品,并删除所有表的用户的所有引用,但我听说 destroy_all 很是过程重,所以我试图 DELETE_ALL 。它不仅能消除从自己的用户表和 ID 所有由空其他表的用户,但保留完好在他们的记录。有人可以分享什么是正确的方法是用于执行这样的任务吗?

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?

我看到 destroy_all 上的所有关联的对象调用破坏函数,但我只是想确认正确的处理方法

I see that destroy_all calls the destroy function on all associated objects but I just want to confirm the correct approach.

推荐答案

你是对的。如果你想删除的用户和所有相关的对象 - >:destroy_all 但是,如果你只是想删除不燮$ P $用户pssing所有相关的对象 - > DELETE_ALL

You are right. If you want to delete the User and all associated objects -> :destroy_all However, if you just want to delete the User without suppressing all associated objects -> delete_all

根据这篇文章:Rails :依赖=> :摧毁VS:依赖=> :DELETE_ALL

:破坏/:destroy_all关联的对象是通过调用其销毁方法,沿着这个对象销毁 :删除/:DELETE_ALL所有相关联的对象,而无需调用立即摧毁了他们的:破坏方法
 
精彩推荐
图片推荐