Ruby-on-rails Rails :dependent => :destroy VS :dependent => :delete_all
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2797339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails :dependent => :destroy VS :dependent => :delete_all
提问by Sergey
In rails guides it's described like this:
在 rails guides 中,它是这样描述的:
Objects will be in addition destroyed if they're associated with
:dependent => :destroy, and deleted if they're associated with:dependent => :delete_all
如果与 相关联,对象将另外被销毁,如果与 相关联,则对象将被
:dependent => :destroy删除:dependent => :delete_all
Right, cool. But what's the difference between being destroyed and being deleted? I tried both and it seems to do the same thing.
没错,酷。但是被销毁和被删除有什么区别呢?我尝试了两者,它似乎做同样的事情。
回答by shingara
The difference is with the callback.
区别在于回调。
The :delete_allis made directly in your application and deletes by SQL :
将:delete_all直接在您的应用程序,并通过SQL删除:
DELETE * FROM users where compagny_id = XXXX
With the :destroy, there is an instantiation of all of your children. So, if you can't destroy it or if each has their own :dependent, its callbacks can be called.
有了:destroy,就有了所有孩子的实例化。所以,如果你不能销毁它或者如果每个人都有自己的:dependent,它的回调可以被调用。
回答by John Topley
On a Rails' model association you can specify the :dependentoption, which can take one of the following three forms:
在 Rails 的模型关联上,您可以指定:dependent选项,它可以采用以下三种形式之一:
:destroy/:destroy_allThe associated objects are destroyed alongside this object by calling theirdestroymethod:delete/:delete_allAll associated objects are destroyed immediately without calling their:destroymethod:nullifyAll associated objects' foreign keys are set toNULLwithout calling theirsavecallbacks
:destroy/:destroy_all关联的对象通过调用它们的destroy方法与此对象一起销毁:delete/:delete_all所有关联的对象都被立即销毁而不调用它们的:destroy方法:nullify所有关联对象的外键都设置为NULL不调用它们的save回调
回答by Manish Shrivastava
See destroy deletes its associated elementswhere delete_all can delete multiple data from self tableas DELETE * FROM table where field = 'xyz'
请参阅destroy 删除其关联元素,其中delete_all 可以从 self 表中删除多个数据作为DELETE * FROM table where field = 'xyz'
:Dependent possible options:
:依赖可能的选项:
Controls what happens to the associated objects when their owner is destroyed. Note that these are implemented as callbacks, and Rails executes callbacks in order. Therefore, other similar callbacks may affect the :dependent behavior, and the :dependentbehavior may affect other callbacks.
控制关联对象的所有者被销毁时会发生什么。请注意,这些是作为回调实现的,Rails 会按顺序执行回调。因此,其他类似的回调可能会影响 :dependent 行为,而该:dependent行为可能会影响其他回调。
:destroycauses all the associated objects to also be destroyed.
:destroy导致所有关联的对象也被销毁。
:delete_allcauses all the associated objects to be deleted directly from the database (so callbacks will not be executed).
:delete_all导致所有关联对象直接从数据库中删除(因此不会执行回调)。
:nullifycauses the foreign keys to be set to NULL. Callbacks are not executed.
:nullify导致外键设置为 NULL。不执行回调。
:restrict_with_exceptioncauses an exception to be raised if there are any associated records.
:restrict_with_exception如果有任何关联的记录,则会导致引发异常。
:restrict_with_errorcauses an error to be added to the owner if there are any associated objects.
:restrict_with_error如果有任何关联的对象,则会导致将错误添加到所有者。
If using with the :throughoption, the association on the join model must be a belongs_to, and the records which get deleted are the join records, rather than the associated records.
如果与:through选项一起使用,连接模型上的关联必须是belongs_to,被删除的记录是连接记录,而不是关联记录。
回答by atlascoder
Actually the main difference is that any callbacks will not be invoked when :delete_allwas used. But when used :destroythe callbacks stack (:after_destroy, :after_commit...) will be fired.
实际上主要的区别在于:delete_all使用时不会调用任何回调。但是当使用:destroy回调堆栈 ( :after_destroy, :after_commit...) 将被触发。
Consequently, if you have touch:ing declarations in models being deleted then it's better to use dependent: :delete_allrather 'dependent: :destroy'.
因此,如果您touch:在模型中删除了 ing 声明,那么最好使用dependent: :delete_all'dependent::destroy'。

