Diff Btw在rails中销毁和删除

时间:2015-04-13 14:28:02

标签: ruby-on-rails

有人可以用示例解释一下diff btw delete和destroy in rails吗?

article.destroy
  

(0.2ms)开始交易
  SQL(0.4ms)DELETE FROM"文章"在哪里"文章"。" id" =? [[" ID&#34 ;,   9]]
  (84.5ms)提交事务

     

=> #

article.delete   
  

SQL(28.3ms)DELETE FROM" articles"在哪里"文章"。" id" = 10

     

=> #

基本上都执行相同的任务,但我无法找出回滚部分。如何在destroy中回滚而不是在delete中回滚。请承担向我解释的负担?

1 个答案:

答案 0 :(得分:-1)

基本上,destroy会在模型上运行任何回调,而删除则不会。

的ActiveRecord :: Persistance.delete

Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted). Returns the frozen instance.

The row is simply removed with an SQL DELETE statement on the record's primary key, and no callbacks are executed.

To enforce the object's before_destroy and after_destroy callbacks or any :dependent association options, use #destroy.

的ActiveRecord :: Persistance.destroy

Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted).

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.