destroy ordering with associations... reasoning?

Hi Andrew,

Andrew Kaspick wrote:

I'm having a problem understanding why rails calls the 'before_destroy' methods in the ordering as as follows (w/contrived ex)...

class User belongs_to :group end

class Group has_many :users, :dependent => :destroy end

If I call destroy on the group...

Group.find(:first).destroy

The ordering of operations is... before_destroy is called for every user in the group first before_destroy is called for the group

The order is driven by the relationships you've specified. The :dependent => destroy on Group tells Rails to destroy any User child records *before* trying to destroy User records. That's a basic referential integrity constraint.

hth, Bill