public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Ensure that save on parent object fails for invalid has_one association. 
Closes #10518. [Pratik]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9232 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
lifo (author)
Sat Apr 05 19:32:51 -0700 2008
commit  7ddc8f2e1bc9c818b622373a8c85bd679533cefd
tree    683b5c10891b4e7704404fcf3de81da08f02a54b
parent  9bc75fd007d56d818b8620569410a20aa92c9fc5
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Ensure that save on parent object fails for invalid has_one association. Closes #10518. [Pratik]
0
+
0
 * Remove duplicate code from associations. [Pratik]
0
 
0
 * Refactor HasManyThroughAssociation to inherit from HasManyAssociation. Association callbacks and <association>_ids= now work with hm:t. #11516 [rubyruy]
...
784
785
786
 
787
788
789
...
1141
1142
1143
 
 
 
 
 
 
 
 
 
 
 
 
1144
1145
1146
...
784
785
786
787
788
789
790
...
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
0
@@ -784,6 +784,7 @@ module ActiveRecord
0
           end
0
           after_save method_name
0
 
0
+ add_single_associated_save_callbacks(reflection.name)
0
           association_accessor_methods(reflection, HasOneAssociation)
0
           association_constructor_method(:build, reflection, HasOneAssociation)
0
           association_constructor_method(:create, reflection, HasOneAssociation)
0
@@ -1141,6 +1142,18 @@ module ActiveRecord
0
           end
0
         end
0
         
0
+ def add_single_associated_save_callbacks(association_name)
0
+ method_name = "validate_associated_records_for_#{association_name}".to_sym
0
+ define_method(method_name) do
0
+ association = instance_variable_get("@#{association_name}")
0
+ if !association.nil?
0
+ errors.add "#{association_name}" unless association.target.nil? || association.valid?
0
+ end
0
+ end
0
+
0
+ validate method_name
0
+ end
0
+
0
         def add_multiple_associated_save_callbacks(association_name)
0
           method_name = "validate_associated_records_for_#{association_name}".to_sym
0
           ivar = "@#{association_name}"
...
433
434
435
 
 
 
 
 
 
 
 
 
 
 
 
436
437
438
...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
0
@@ -433,6 +433,18 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
0
     assert_equal a, firm.account
0
     assert_equal a, firm.account(true)
0
   end
0
+
0
+ def test_save_fails_for_invalid_has_one
0
+ firm = Firm.find(:first)
0
+ assert firm.valid?
0
+
0
+ firm.account = Account.new
0
+
0
+ assert !firm.account.valid?
0
+ assert !firm.valid?
0
+ assert !firm.save
0
+ assert_equal "is invalid", firm.errors.on("account")
0
+ end
0
 
0
   def test_assignment_before_either_saved
0
     firm = Firm.new("name" => "GlobalMegaCorp")

Comments

    No one has commented yet.