mirror of
https://github.com/github/rails.git
synced 2026-01-26 23:08:58 -05:00
Fix that has_one natural assignment to already associated record. [#854 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
@@ -21,8 +21,8 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
@@ -79,6 +79,16 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user