:dependent for not destroying

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!

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.

No it doesn’t.

You’ll need to build your own before_destroy validation method for this.

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. [email protected]

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

http://agilewebdevelopment.com/plugins/dependent_protect_option


Amaia C.
Dabne Tecnologías de la Información - www.dabne.net -
http://blog.dabne.net
*
Buscador de Subvenciones - http://buscaboe.dabne.net
*

2007/12/31, Robert W. [email protected]: