Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix that has_one natural assignment to already associated record. [#854

… state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit ea40f71431a821b2ddb37be6ea3ee7d8dac63b85 1 parent 7e4ea5f
Jakub Kuźma authored lifo committed
View
4 activerecord/lib/active_record/associations/has_one_association.rb
@@ -21,8 +21,8 @@ def build(attrs = {}, replace_existing = true)
def replace(obj, dont_save = false)
load_target
- unless @target.nil?
- if dependent? && !dont_save && @target != obj
+ unless @target.nil? || @target == obj
+ if dependent? && !dont_save
@target.destroy unless @target.new_record?
@owner.clear_association_cache
else
View
10 activerecord/test/cases/associations/has_one_associations_test.rb
@@ -79,6 +79,16 @@ def test_natural_assignment_to_nil
assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
end
+ def test_natural_assignment_to_already_associated_record
+ company = companies(:first_firm)
+ account = accounts(:signals37)
+ assert_equal company.account, account
+ company.account = account
+ company.reload
+ account.reload
+ assert_equal company.account, account
+ end
+
def test_assignment_without_replacement
apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10)

1 comment on commit ea40f71

@ncr

Congrats Jakub, waiting for more :)

Please sign in to comment.
Something went wrong with that request. Please try again.