# Why isn't there a :dependent =\> false option?

**URL:** <https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771>\
**Category:** Rails\
**Created:** [December 16, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771 "2008-12-16T16:45:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/1 "2008-12-16T16:45:09Z")

</div>

I have an “audit” table that records when changes are made to another  
table. If a record is destroyed, I don’t want all the change audits  
for that record destroyed or nullified along with it.

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/2 "2008-12-16T17:38:35Z")

</div>

On Tue, Dec 16, 2008 at 10:29 AM, Ed Lebert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an “audit” table that records when changes are made to another  
> table. If a record is destroyed, I don’t want all the change audits  
> for that record destroyed or nullified along with it.
> 
> How about :dependent =\> :nullify, or perhaps not attaching :dependent at  
> all to your model which (I am presuming) :has\_many of the other model?

–wpd

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/3 "2008-12-16T17:43:52Z")

</div>

@Ed:

Just leave the :dependent option off completely and nothing happens to  
the other records. That’s the default behavior.

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/4 "2008-12-16T19:30:55Z")

</div>

Robert W. wrote:

> I think a better question is, “Why isn’t there a :dependent =\> :deny  
> option?”

Because Josh said no.

[http://dev.rubyonrails.org/ticket/3837](http://dev.rubyonrails.org/ticket/3837)

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/5 "2008-12-16T18:32:05Z")

</div>

I think a better question is, “Why isn’t there a :dependent =\> :deny  
option?”

It’s very common to have a relational dependency where you want to deny  
the deletion of an object that has related objects. It would be really  
nice if this referential integrity rule was part of Rails core rather  
than having to write it yourself or use a plugin.

It doesn’t seem to me that it would be too difficult to add, if the  
count of the related objects is greater than zero deny the deletion.

Example:

Employee  
belongs\_to :department

Department  
has\_many :employees, :dependent =\> :deny

Deny deletion of a department if the department contains one or more  
employees.

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/6 "2008-12-16T19:52:54Z")

</div>

> Department  
> has\_many :employees, :dependent =\> :deny

has\_many :employees, :dependent =\> ‘something’

indicates what non-default behavior to apply to the :employees… not  
some behavior related to the :department

we already have a :before\_destroy callback to serve your purpose, which  
is far more powerful – not every case is as simple as “Don’t delete if  
there are any employees”

---

<div class="post-metadata">

**Author:** ![Ed\_Lebert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@Ed\_Lebert](https://www.ruby-forum.com/u/Ed_Lebert)\
**Post date:** [December 16, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/why-isnt-there-a-dependent-false-option/155771/7 "2008-12-16T20:12:38Z")

</div>

James B. wrote:

> Robert W. wrote:
> 
> > I think a better question is, “Why isn’t there a :dependent =\> :deny  
> > option?”
> 
> Because Josh said no.
> 
> [http://dev.rubyonrails.org/ticket/3837](http://dev.rubyonrails.org/ticket/3837)

That’s a good answer, I’ll accept that.

Ar Chron wrote:

> we already have a :before\_destroy callback to serve your purpose, which  
> is far more powerful – not every case is as simple as “Don’t delete if  
> there are any employees”

This also makes sense. Especially in the context of Rails. Having a  
“deny delete” or “protect from delete” as implemented in other  
frameworks I’ve used also makes sense in their own right. But, it is  
true that this is more a validation issue than an association issue, so  
also makes sense to be implemented in the context of validation. The  
line can get a bit fuzzy when talking about “relational integrity” since  
it takes both association manipulation (cascade delete and nullify) and  
validation (deny delete) in order to fully implement.
