From 217a8b01b1349d50fc9c331e9411034289c640ab Mon Sep 17 00:00:00 2001 From: James Coleman Date: Fri, 2 Mar 2012 15:37:48 -0500 Subject: Unset association when existing record is destroyed. To avoid foreign key errors (and invalid data) in the database, when a belongs_to association is destroyed, it should also be nil'd out on the parent object. --- activerecord/lib/active_record/autosave_association.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record/autosave_association.rb') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index d468663084..0468a73c7d 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -402,6 +402,7 @@ module ActiveRecord autosave = reflection.options[:autosave] if autosave && record.marked_for_destruction? + self[reflection.foreign_key] = nil record.destroy elsif autosave != false saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?) -- cgit v1.2.3