mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
has_one should always remove the old record (properly), even if not saving the new record, so we don't get the database into a pickle
This commit is contained in:
parent
665880c080
commit
c6e10b0f60
4 changed files with 31 additions and 11 deletions
|
@ -20,7 +20,7 @@ module ActiveRecord
|
||||||
load_target
|
load_target
|
||||||
|
|
||||||
if @target && @target != record
|
if @target && @target != record
|
||||||
remove_target(save && @reflection.options[:dependent])
|
remove_target(@reflection.options[:dependent])
|
||||||
end
|
end
|
||||||
|
|
||||||
if record
|
if record
|
||||||
|
|
|
@ -2,9 +2,11 @@ require "cases/helper"
|
||||||
require 'models/developer'
|
require 'models/developer'
|
||||||
require 'models/project'
|
require 'models/project'
|
||||||
require 'models/company'
|
require 'models/company'
|
||||||
|
require 'models/ship'
|
||||||
|
require 'models/pirate'
|
||||||
|
|
||||||
class HasOneAssociationsTest < ActiveRecord::TestCase
|
class HasOneAssociationsTest < ActiveRecord::TestCase
|
||||||
fixtures :accounts, :companies, :developers, :projects, :developers_projects
|
fixtures :accounts, :companies, :developers, :projects, :developers_projects, :ships, :pirates
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
Account.destroyed_account_ids.clear
|
Account.destroyed_account_ids.clear
|
||||||
|
@ -164,15 +166,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal account, firm.account
|
assert_equal account, firm.account
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_build_association_twice_without_saving_affects_nothing
|
|
||||||
count_of_account = Account.count
|
|
||||||
firm = Firm.find(:first)
|
|
||||||
firm.build_account("credit_limit" => 1000)
|
|
||||||
firm.build_account("credit_limit" => 2000)
|
|
||||||
|
|
||||||
assert_equal count_of_account, Account.count
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_create_association
|
def test_create_association
|
||||||
firm = Firm.create(:name => "GlobalMegaCorp")
|
firm = Firm.create(:name => "GlobalMegaCorp")
|
||||||
account = firm.create_account(:credit_limit => 1000)
|
account = firm.create_account(:credit_limit => 1000)
|
||||||
|
@ -293,4 +286,26 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
|
||||||
new_account = companies(:first_firm).build_account(:firm_name => 'Account')
|
new_account = companies(:first_firm).build_account(:firm_name => 'Account')
|
||||||
assert_equal new_account.firm_name, "Account"
|
assert_equal new_account.firm_name, "Account"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_creation_failure_without_dependent_option
|
||||||
|
pirate = pirates(:blackbeard)
|
||||||
|
orig_ship = pirate.ship.target
|
||||||
|
|
||||||
|
assert_equal ships(:black_pearl), orig_ship
|
||||||
|
new_ship = pirate.create_ship
|
||||||
|
assert_not_equal ships(:black_pearl), new_ship
|
||||||
|
assert_equal new_ship, pirate.ship
|
||||||
|
assert new_ship.new_record?
|
||||||
|
assert_nil orig_ship.pirate_id
|
||||||
|
assert !orig_ship.changed? # check it was saved
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_creation_failure_with_dependent_option
|
||||||
|
pirate = pirates(:blackbeard).becomes(DestructivePirate)
|
||||||
|
orig_ship = pirate.dependent_ship.target
|
||||||
|
|
||||||
|
new_ship = pirate.create_dependent_ship
|
||||||
|
assert new_ship.new_record?
|
||||||
|
assert orig_ship.destroyed?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
1
activerecord/test/fixtures/ships.yml
vendored
1
activerecord/test/fixtures/ships.yml
vendored
|
@ -1,5 +1,6 @@
|
||||||
black_pearl:
|
black_pearl:
|
||||||
name: "Black Pearl"
|
name: "Black Pearl"
|
||||||
|
pirate: blackbeard
|
||||||
interceptor:
|
interceptor:
|
||||||
id: 2
|
id: 2
|
||||||
name: "Interceptor"
|
name: "Interceptor"
|
||||||
|
|
|
@ -78,3 +78,7 @@ class Pirate < ActiveRecord::Base
|
||||||
ship_log << "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"
|
ship_log << "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DestructivePirate < Pirate
|
||||||
|
has_one :dependent_ship, :class_name => 'Ship', :foreign_key => :pirate_id, :dependent => :destroy
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue