1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #5248 from jcoleman/should-unset-association-when-an-existing-record-is-destroyed

Unset association when existing record is destroyed.
This commit is contained in:
Jon Leighton 2012-09-21 02:40:26 -07:00
commit 834d6da54e
2 changed files with 17 additions and 0 deletions

View file

@ -394,6 +394,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?)

View file

@ -463,6 +463,22 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
end
end
def test_should_unset_association_when_an_existing_record_is_destroyed
@ship.reload
original_pirate_id = @ship.pirate.id
@ship.attributes = {:pirate_attributes => {:id => @ship.pirate.id, :_destroy => true}}
@ship.save!
assert_empty Pirate.where(["id = ?", original_pirate_id])
assert_nil @ship.pirate_id
assert_nil @ship.pirate
@ship.reload
assert_empty Pirate.where(["id = ?", original_pirate_id])
assert_nil @ship.pirate_id
assert_nil @ship.pirate
end
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth|
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth })