问题描述
@ model.destroy
和 @model有什么区别?删除
例如:
Model.find_by(col: "foo").destroy_all
//and
Model.find_by(col: "foo").delete_all
如果我使用一个或另一个,真的有关系吗?
Does it really matter if I use the one or the other?
推荐答案
基本上 destroy
在模型上运行任何回调,而删除
不运行。
Basically destroy
runs any callbacks on the model while delete
doesn't.
从:
仅通过在记录的主键上使用SQL DELETE语句删除该行,并且不执行任何回调。
The row is simply removed with an SQL DELETE statement on the record's primary key, and no callbacks are executed.
要强制执行对象的before_destroy和after_destroy回调或任何:dependent关联选项,请使用#destroy。
To enforce the object's before_destroy and after_destroy callbacks or any :dependent association options, use #destroy.
有一系列与destroy相关的回调。如果before_destroy回调返回false,则取消操作,destroy返回false。有关更多详细信息,请参见ActiveRecord :: Callbacks。
There's a series of callbacks associated with destroy. If the before_destroy callback return false the action is cancelled and destroy returns false. See ActiveRecord::Callbacks for further details.
这篇关于销毁和删除之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!