aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-03-03 06:25:39 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-03-03 06:25:39 +0000
commit116658e69b9c4a722e6ae8717629b8cd0057db89 (patch)
tree998f82f091355374ba02f79071e6b5990ac8b713
parent9ed7430b9489dc1e8e695a065fe34e319e3ba2b4 (diff)
downloadrails-116658e69b9c4a722e6ae8717629b8cd0057db89.tar.gz
rails-116658e69b9c4a722e6ae8717629b8cd0057db89.tar.bz2
rails-116658e69b9c4a722e6ae8717629b8cd0057db89.zip
Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen (closes #4058) [anna@wota.jp]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3753 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb2
-rwxr-xr-xactiverecord/test/associations_test.rb9
3 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 0a27f18262..d2d4c08b5e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen #4058 [anna@wota.jp]
+
* Added Sybase database adapter that relies on the Sybase Open Client bindings (see http://raa.ruby-lang.org/project/sybase-ctlib) #3765 [John Sheets]. It's almost completely Active Record compliant, but has the following caveats:
* Does not support DATE SQL column types; use DATETIME instead.
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index d07323a869..65f584c6f9 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -34,7 +34,7 @@ module ActiveRecord
@owner.clear_association_cache
else
@target[@reflection.primary_key_name] = nil
- @target.save unless @owner.new_record?
+ @target.save unless @owner.new_record? || @target.new_record?
end
end
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 79c7cc8120..c6cf7807fa 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -158,6 +158,15 @@ class HasOneAssociationsTest < Test::Unit::TestCase
assert_equal "can't be empty", account.errors.on("credit_limit")
end
+ def test_build_association_twice_without_saving_affects_nothing
+ count_of_account = Account.count
+ firm = Firm.find(:first)
+ account1 = firm.build_account("credit_limit" => 1000)
+ account2 = firm.build_account("credit_limit" => 2000)
+
+ assert_equal count_of_account, Account.count
+ end
+
def test_create_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save