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:
commit
834d6da54e
2 changed files with 17 additions and 0 deletions
|
@ -394,6 +394,7 @@ module ActiveRecord
|
||||||
autosave = reflection.options[:autosave]
|
autosave = reflection.options[:autosave]
|
||||||
|
|
||||||
if autosave && record.marked_for_destruction?
|
if autosave && record.marked_for_destruction?
|
||||||
|
self[reflection.foreign_key] = nil
|
||||||
record.destroy
|
record.destroy
|
||||||
elsif autosave != false
|
elsif autosave != false
|
||||||
saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
|
saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
|
||||||
|
|
|
@ -463,6 +463,22 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
|
||||||
[nil, '0', 0, 'false', false].each do |not_truth|
|
[nil, '0', 0, 'false', false].each do |not_truth|
|
||||||
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth })
|
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth })
|
||||||
|
|
Loading…
Reference in a new issue