分享 rails delete 和 delete_all 方法注意

lhy20062008 · March 05, 2014 · Last by lhy20062008 replied at March 10, 2014 · 5497 hits

在 rails 中,最近发现删除一个 record 有 2 中情况: 1、直接从数据库删除 User.where(id: 1).delete_all,产生的 sql 语句类似这样: delete from users where id = 1 2、删除关联关系,不删除数据 A.first.users.delete_all 产生的 sql 语句如下: update from users set a_id = 0 where a_id = 1 以上 sql 有误,暂时如吃,请大家多多指正批评。

官方手册有提示到那些会触发 callback,那些方法会跳过 http://guides.rubyonrails.org/active_record_callbacks.html#running-callbacks

#1 楼 @cxh116 学习了,的确是这样的,delete 不触发回调,destroy 能够删除数据,而不仅仅是删除关联关系。

You need to Sign in before reply, if you don't have an account, please Sign up first.