public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make sure associated has_many/habtm objects get saved even when :validate => 
false is used. [#486 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
DefV (author)
Wed Jun 25 03:42:33 -0700 2008
lifo (committer)
Thu Jun 26 19:02:13 -0700 2008
commit  b2b761166d28c1aba9165da76fba28027171fd2d
tree    c2a6de3d5fcd63286e77e4e95e2e3374cd9c2e83
parent  5ca7d01ecaa5e97f724169c0177027d0d85066da
...
711
712
713
714
 
 
715
716
717
...
801
802
803
804
 
805
806
807
...
940
941
942
943
 
944
945
946
...
1043
1044
1045
1046
 
 
1047
1048
1049
...
1163
1164
1165
1166
 
1167
1168
1169
...
1175
1176
1177
1178
 
1179
1180
1181
...
1196
1197
1198
 
 
 
 
1199
1200
1201
...
1217
1218
1219
1220
1221
1222
1223
...
711
712
713
 
714
715
716
717
718
...
802
803
804
 
805
806
807
808
...
941
942
943
 
944
945
946
947
...
1044
1045
1046
 
1047
1048
1049
1050
1051
...
1165
1166
1167
 
1168
1169
1170
1171
...
1177
1178
1179
 
1180
1181
1182
1183
...
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
...
1223
1224
1225
 
1226
1227
1228
0
@@ -711,7 +711,8 @@ module ActiveRecord
0
 
0
         configure_dependency_for_has_many(reflection)
0
 
0
-        add_multiple_associated_save_callbacks(reflection.name) unless options[:validate] == false
0
+        add_multiple_associated_validation_callbacks(reflection.name) unless options[:validate] == false
0
+        add_multiple_associated_save_callbacks(reflection.name)
0
         add_association_callbacks(reflection.name, reflection.options)
0
 
0
         if options[:through]
0
@@ -801,7 +802,7 @@ module ActiveRecord
0
           end
0
           after_save method_name
0
 
0
-          add_single_associated_save_callbacks(reflection.name) if options[:validate] == true
0
+          add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
0
           association_accessor_methods(reflection, HasOneAssociation)
0
           association_constructor_method(:build,  reflection, HasOneAssociation)
0
           association_constructor_method(:create, reflection, HasOneAssociation)
0
@@ -940,7 +941,7 @@ module ActiveRecord
0
           )
0
         end
0
 
0
-        add_single_associated_save_callbacks(reflection.name) if options[:validate] == true
0
+        add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
0
 
0
         configure_dependency_for_belongs_to(reflection)
0
       end
0
@@ -1043,7 +1044,8 @@ module ActiveRecord
0
       def has_and_belongs_to_many(association_id, options = {}, &extension)
0
         reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension)
0
 
0
-        add_multiple_associated_save_callbacks(reflection.name) unless options[:validate] == false
0
+        add_multiple_associated_validation_callbacks(reflection.name) unless options[:validate] == false
0
+        add_multiple_associated_save_callbacks(reflection.name)
0
         collection_accessor_methods(reflection, HasAndBelongsToManyAssociation)
0
 
0
         # Don't use a before_destroy callback since users' before_destroy
0
@@ -1163,7 +1165,7 @@ module ActiveRecord
0
           end
0
         end
0
         
0
-        def add_single_associated_save_callbacks(association_name)
0
+        def add_single_associated_validation_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
@@ -1175,7 +1177,7 @@ module ActiveRecord
0
           validate method_name
0
         end
0
         
0
-        def add_multiple_associated_save_callbacks(association_name)
0
+        def add_multiple_associated_validation_callbacks(association_name)
0
           method_name = "validate_associated_records_for_#{association_name}".to_sym
0
           ivar = "@#{association_name}"
0
 
0
@@ -1196,6 +1198,10 @@ module ActiveRecord
0
           end
0
 
0
           validate method_name
0
+        end
0
+
0
+        def add_multiple_associated_save_callbacks(association_name)
0
+          ivar = "@#{association_name}"
0
 
0
           method_name = "before_save_associated_records_for_#{association_name}".to_sym
0
           define_method(method_name) do
0
@@ -1217,7 +1223,6 @@ module ActiveRecord
0
             else
0
               []
0
             end
0
-
0
             records_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank?
0
 
0
             # reconstruct the SQL queries now that we know the owner's id
...
345
346
347
348
 
349
350
351
...
353
354
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
357
358
...
345
346
347
 
348
349
350
351
...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
0
@@ -345,7 +345,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
   def test_invalid_adding_with_validate_false
0
     firm = Firm.find(:first)
0
     client = Client.new
0
-    firm.unvalidated_clients_of_firm << Client.new
0
+    firm.unvalidated_clients_of_firm << client
0
 
0
     assert firm.valid?
0
     assert !client.valid?
0
@@ -353,6 +353,23 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert client.new_record?
0
   end
0
 
0
+  def test_valid_adding_with_validate_false
0
+    no_of_clients = Client.count
0
+
0
+    firm = Firm.find(:first)
0
+    client = Client.new("name" => "Apple")
0
+
0
+    assert firm.valid?
0
+    assert client.valid?
0
+    assert client.new_record?
0
+
0
+    firm.unvalidated_clients_of_firm << client
0
+
0
+    assert firm.save
0
+    assert !client.new_record?
0
+    assert_equal no_of_clients+1, Client.count
0
+  end
0
+
0
   def test_build
0
     company = companies(:first_firm)
0
     new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }

Comments