Fix to make has_one/has_many :dependent behaviour consistent (my first patch)

I've just submitted my first patch, and I'd love to get some feedback
on it. This patching Rails thing is a bit daunting if you haven't done
it before! Not sure if I'm on the right track.

From the description:

A has_one or has_many relationship with :dependent set either :nullify
will incorrectly destroy the associated objects when the association
is reassigned or set to nil. Instead, the associated objects' foreign
keys should be nullified, as they are when the parent object is

For example, given this association:

  class Fred
    has_one :george, :dependent => :nullify

The following will cause george to be destroyed.

  fred = Fred.create
  george = George.create(:fred_id => fred) = nil

The issue also extends to :dependent => :delete and :dependent
=> :delete_all. Associated objects are destroyed rather than deleted
when the association is reassigned.

The attached patch fixes this issue and makes :dependent behaviour
consistent in all circumstances the I can think of.


Pete Yandell

D'oh! And I just realised I made a typo in the first line of the
description. It should read:

  A has_one or has_many relationship with :dependent set

It's been a long day.

Pete Yandell

Hey Pete,

Looks like a good patch, but I think they're already discussing the
issue here:

It certainly seems to cover some of the same ground (must admit I
haven't looked into it in too much detail). Maybe check that ticket
out and see if there's anything to add to it from your patch?


Saw that, but it only addresses has_many relationships, and changes
the default behaviour when :dependent isn't set in a way that might
break things.

Thanks for reminding me of that though...I'll at least add some links
between the tickets.

Pete Yandell