aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/associations_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-06 21:10:59 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-06 21:10:59 +0000
commit2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc (patch)
treeee3ae5d9b837bc74112e1e699aa1e73f3dd95804 /activerecord/test/associations_test.rb
parent5c2e0fe6491761ba2c55b780663d7f9ec86a62cc (diff)
downloadrails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.tar.gz
rails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.tar.bz2
rails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.zip
Added a second parameter to the build and create method for has_one that controls whether the existing association should be replaced (which means nullifying its foreign key as well). By default this is true, but false can be passed to prevent it.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1392 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/associations_test.rb')
-rwxr-xr-xactiverecord/test/associations_test.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 03fd2b263d..7269c10c1d 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -97,6 +97,35 @@ class HasOneAssociationsTest < Test::Unit::TestCase
# account is dependent, therefore is destroyed when reference to owner is lost
assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
end
+
+ def test_assignment_without_replacement
+ apple = Firm.create("name" => "Apple")
+ citibank = Account.create("credit_limit" => 10)
+ apple.account = citibank
+ assert_equal apple.id, citibank.firm_id
+
+ hsbc = apple.build_account({ :credit_limit => 20}, false)
+ assert_equal apple.id, hsbc.firm_id
+ hsbc.save
+ assert_equal apple.id, citibank.firm_id
+
+ nykredit = apple.create_account({ :credit_limit => 30}, false)
+ assert_equal apple.id, nykredit.firm_id
+ assert_equal apple.id, citibank.firm_id
+ assert_equal apple.id, hsbc.firm_id
+ end
+
+ def test_assignment_without_replacement_on_create
+ apple = Firm.create("name" => "Apple")
+ citibank = Account.create("credit_limit" => 10)
+ apple.account = citibank
+ assert_equal apple.id, citibank.firm_id
+
+ hsbc = apple.create_account({ :name => "HSBC", :credit_limit => 10}, false)
+ assert_equal apple.id, hsbc.firm_id
+ hsbc.save
+ assert_equal apple.id, citibank.firm_id
+ end
def test_dependence
firm = Firm.find(1)