# :dependent for not destroying

**URL:** <https://www.ruby-forum.com/t/dependent-for-not-destroying/124571>\
**Category:** Rails\
**Created:** [December 30, 2007, 11:34am UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571 "2007-12-30T11:34:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpflaum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@rpflaum](https://www.ruby-forum.com/u/rpflaum)\
**Post date:** [December 30, 2007, 11:34am UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571/1 "2007-12-30T11:34:04Z")

</div>

Hi,

Is there a way to use the :dependent option to set a certain attribute  
to null rather than destroying the records?

For example, take the following models:

User  
id, integer  
name, string  
group\_id, integer

belongs\_to :democrats

Groups  
id, integer  
name, string

has\_many :users

If I delete a certain group, is there a way to have the ‘group\_id’  
attribute of all the users affiliated with the deleted group be set to  
null rather than be destroyed?

Thank you!

---

<div class="post-metadata">

**Author:** ![rpflaum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@rpflaum](https://www.ruby-forum.com/u/rpflaum)\
**Post date:** [December 30, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571/2 "2007-12-30T12:09:41Z")

</div>

Ooooh, I’m so excited. I found my answer. :dependent =\> :nullify will do  
what I want. I hope this will help some person in the future.

---

<div class="post-metadata">

**Author:** ![rpflaum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@rpflaum](https://www.ruby-forum.com/u/rpflaum)\
**Post date:** [December 31, 2007, 2:41am UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571/3 "2007-12-31T02:41:05Z")

</div>

No it doesn’t.

You’ll need to build your own before\_destroy validation method for this.

---

<div class="post-metadata">

**Author:** ![rpflaum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@rpflaum](https://www.ruby-forum.com/u/rpflaum)\
**Post date:** [December 31, 2007, 2:35am UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571/4 "2007-12-31T02:35:50Z")

</div>

Now if there was just a built-in way to have something like :dependent  
=\> :deny we’d be set.

By the way, what is the best way to support the “deny delete”  
association rule. Just to clarify this is saying “Deny deletion of the  
parent object if it has one or more children.” This rule is often  
useful to ensure referential integrity.

Example:  
Scenario 1: An admin creates a new product with item number 1010. It  
is realized later that the item will never be sold. Since no orders of  
the product exist in the system it’s fine to delete the item.

Scenario 2: It is discovered that an existing product with item number  
1001 is no longer available to be sold. The admin attempts to delete  
the existing product, but there are outstanding orders containing  
product 1001. It’s not good to use either :dependent =\> :destroy  
nor :dependent =\> :nullify in this scenario. You can’t simply let the  
system automatically delete all orders containing product 1001. You  
must first resolve the orders (child objects) before deleting the  
product object.

Does Rails have built-in support for this common referential integrity  
rule?

On Dec 30, 6:09 am, Rick P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rpflaum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@rpflaum](https://www.ruby-forum.com/u/rpflaum)\
**Post date:** [December 31, 2007, 11:58am UTC](https://www.ruby-forum.com/t/dependent-for-not-destroying/124571/5 "2007-12-31T11:58:07Z")

</div>

If I understood you correctly I think this plugins does what you want.

[http://agilewebdevelopment.com/plugins/dependent\_protect\_option](http://agilewebdevelopment.com/plugins/dependent_protect_option)

–  
Amaia C.  
Dabne TecnologÃ­as de la InformaciÃ³n - [www.dabne.net](http://www.dabne.net) -  
[http://blog.dabne.net](http://blog.dabne.net)  
~~\*~~  
Buscador de Subvenciones - [http://buscaboe.dabne.net](http://buscaboe.dabne.net)  
~~\*~~

2007/12/31, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
