本文介绍了使ActiveRecord中的依赖对象无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:dependent => :nullify

我为什么要使从属对象无效,因为我看不到孤立数据库记录的目的.

Why would I want to nullify dependant objects, since I don't see a purpose by orphaning database records.

推荐答案

无效化仅在非常特殊的情况下有用;举例来说,假设您有一些项目,可能只有一个项目,而一个项目则只能对该项目进行调查(因此,引用该项目的代理的外键字段可以为空).如果业务代表放弃了他处理的所有调查(假设他已被解雇),则您不想销毁项目记录,但是您不能让它引用不再存在的业务代表记录,因此您将其取消它的外键字段.

Nullifying is only usefull in very specific cases ; let's say for example you have some projects, which may or may not be surveyed by one and only one an agent (so its foreign key field referencing the agent can be null). If the agent abandons all the surveys he handles (let's say he's been fired), you don't want to destroy the project record, but you can't let it reference to an agent record that won't exist anymore, so you nullify its foreign key field.

这篇关于使ActiveRecord中的依赖对象无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:15