Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5209 (closed defect: duplicate)

Opened 3 years ago

Last modified 2 years ago

[PATCH] Consistent behaviour for has_one/has_many methods when using :dependent => :nullify

Reported by: chris@feedmechocolate.com Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 1.1.1
Severity: normal Keywords:
Cc:

Description

The :dependent => :nullify option currently gives inconsistent behaviour. It will nullify the child records when the parent is deleted, as expected. But in other situations it will delete the child records rather than nullifying them. For example:

For a has_many :children, :dependent => :nullify, the following statements result in deleted children:

  • parent.children.delete(a_child)
  • parent.children.clear

And for a has_one :child, :dependent => :nullify, the following statement results in a deleted child:

  • parent.child = nil

If I understand the idea of :nullify correctly, you would expect these statements to nullify the child[ren], not delete them.

This patch (with tests) attempts to fix the problem by doing some explicit checking for the :nullify option, and reverting back to the normal non-dependent behaviour (i.e. just nullifying the foreign keys) where appropriate.

Attachments

consistent_behaviour_for_dependent_nullify.diff (6.0 kB) - added by chris@feedmechocolate.com on 05/27/06 01:21:48.
consistent_behaviour_for_dependent_nullify.2.diff (5.0 kB) - added by chrismear on 12/19/06 09:08:14.
Updated for r5746

Change History

05/27/06 01:21:48 changed by chris@feedmechocolate.com

  • attachment consistent_behaviour_for_dependent_nullify.diff added.

08/21/06 03:56:06 changed by Chris Mear <chris@feedmechocolate.com>

  • version set to 1.1.1.

08/21/06 04:12:59 changed by Chris Mear <chris@feedmechocolate.com>

Just tested against the latest edge revision (r4799). The behaviour is still as described above, and the patch is still valid.

(in reply to: ↑ description ) 12/18/06 13:40:02 changed by Catfish

This is something that caught me out, too. Patch looks good to me - do we have a good reason for not applying it?

12/19/06 09:08:14 changed by chrismear

  • attachment consistent_behaviour_for_dependent_nullify.2.diff added.

Updated for r5746

06/30/07 03:07:10 changed by nzkoz

  • status changed from new to closed.
  • resolution set to duplicate.

Further discussion on this should be in #7309