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?
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?