导轨:依赖=>:destroy VS :dependent =>:删除所有 [英] Rails :dependent => :destroy VS :dependent => :delete_all

查看:17
本文介绍了导轨:依赖=>:destroy VS :dependent =>:删除所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 指南中是这样描述的:

In rails guides it's described like this:

如果对象与 :dependent =>:destroy,如果它们与 :dependent => 关联则删除:delete_all

Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :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.

推荐答案

区别在于回调.

:delete_all 直接在您的应用程序中生成并通过 SQL 删除:

The :delete_all is made directly in your application and deletes by SQL :

DELETE * FROM users where compagny_id = XXXX

使用 :destroy,您的所有孩子都会被实例化.所以,如果你不能销毁它,或者如果每个都有自己的 :dependent,它的回调可以被调用.

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 VS :dependent =>:删除所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆