aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/associations_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-12 05:34:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-12 05:34:10 +0000
commit3b9e90a4dab94d25bfa7ed5e47ca0641857ef992 (patch)
tree1bd1c5d02280f93c447d1917da98cbbdf9ab31a8 /activerecord/test/associations_test.rb
parentb59619600fbbfb2cf6d7156fb4c21bb2c0cec955 (diff)
downloadrails-3b9e90a4dab94d25bfa7ed5e47ca0641857ef992.tar.gz
rails-3b9e90a4dab94d25bfa7ed5e47ca0641857ef992.tar.bz2
rails-3b9e90a4dab94d25bfa7ed5e47ca0641857ef992.zip
Moved build_association and create_association for has_one and belongs_to out of deprecation as they work when the association is nil unlike association.build and association.create, which require the association to be already in place #864
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1146 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/associations_test.rb')
-rwxr-xr-xactiverecord/test/associations_test.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 71e2ab01f8..6cca750644 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -105,6 +105,30 @@ class HasOneAssociationsTest < Test::Unit::TestCase
assert_equal 1, Account.find_all.length
end
+ def test_succesful_build_association
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.save
+
+ account = firm.build_account("credit_limit" => 1000)
+ assert account.save
+ assert_equal account, firm.account
+ end
+
+ def test_failing_build_association
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.save
+
+ account = firm.build_account
+ assert !account.save
+ assert_equal "can't be empty", account.errors.on("credit_limit")
+ end
+
+ def test_create_association
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.save
+ assert_equal firm.create_account("credit_limit" => 1000), firm.account
+ end
+
def test_build
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
@@ -563,6 +587,19 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
assert_equal apple.id, citibank.firm_id
end
+ def test_creating_the_belonging_object
+ citibank = Account.create("credit_limit" => 10)
+ apple = citibank.create_firm("name" => "Apple")
+ assert_equal apple, citibank.firm
+ end
+
+ def test_building_the_belonging_object
+ citibank = Account.create("credit_limit" => 10)
+ apple = citibank.build_firm("name" => "Apple")
+ citibank.save
+ assert_equal apple.id, citibank.firm_id
+ end
+
def test_natural_assignment_to_nil
client = Client.find(3)
client.firm = nil