且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails:删除级联与依赖破坏

更新时间:2023-02-05 11:38:37

这实际上取决于您想要的行为。在情况1中,将在每个关联的订单上调用destroy,因此, ActiveRecord回调。在情况2中,不会触发这些回调,但是会更快,并且可以确保引用完整性。

It really depends on the behavior you want. In case 1, destroy will be called on each associated order, and therefor so will the ActiveRecord callbacks. In case 2, these callbacks are not triggered, but it will be way faster and guarantees referential integrity.

在应用程序初期,我建议使用:dependent => :destroy 因为它使您可以以独立于数据库的方式进行开发。一旦开始扩展,出于性能/完整性的原因,应该开始在数据库中进行扩展。

In an application's infancy, I'd recommend going with :dependent => :destroy because it lets you develop in a way that is independent of the database. Once you start to scale, you should start doing it in the database for performance/integrity reasons.