Navigation Menu

Skip to content

Commit

Permalink
Ensure the parent record is always saved when the child is invalid. [#…
Browse files Browse the repository at this point in the history
…2249 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
alloy authored and lifo committed Apr 27, 2009
1 parent 21aa326 commit da3c21e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -311,11 +311,13 @@ def save_collection_association(reflection)
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
def save_has_one_association(reflection)
if (association = association_instance_get(reflection.name)) && !association.target.nil?
if reflection.options[:autosave] && association.marked_for_destruction?
autosave = reflection.options[:autosave]

if autosave && association.marked_for_destruction?
association.destroy
elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || reflection.options[:autosave]
elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave
association[reflection.primary_key_name] = id
association.save(false)
association.save(!autosave)
end
end
end
Expand All @@ -330,10 +332,12 @@ def save_has_one_association(reflection)
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
def save_belongs_to_association(reflection)
if association = association_instance_get(reflection.name)
if reflection.options[:autosave] && association.marked_for_destruction?
autosave = reflection.options[:autosave]

if autosave && association.marked_for_destruction?
association.destroy
else
association.save(false) if association.new_record? || reflection.options[:autosave]
association.save(!autosave) if association.new_record? || autosave

if association.updated?
self[reflection.primary_key_name] = association.id
Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -38,6 +38,17 @@ def base
end

class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
def test_should_save_parent_but_not_invalid_child
firm = Firm.new(:name => 'GlobalMegaCorp')
assert firm.valid?

firm.build_account_using_primary_key
assert !firm.build_account_using_primary_key.valid?

assert firm.save
assert firm.account_using_primary_key.new_record?
end

def test_save_fails_for_invalid_has_one
firm = Firm.find(:first)
assert firm.valid?
Expand Down Expand Up @@ -126,6 +137,17 @@ def test_not_resaved_when_unchanged
end

class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_save_parent_but_not_invalid_child
client = Client.new(:name => 'Joe (the Plumber)')
assert client.valid?

client.build_firm
assert !client.firm.valid?

assert client.save
assert client.firm.new_record?
end

def test_save_fails_for_invalid_belongs_to
assert log = AuditLog.create(:developer_id => 0, :message => "")

Expand Down

0 comments on commit da3c21e

Please sign in to comment.