Skip to content
This repository
Browse code

Make sure associated has_many/habtm objects get saved even when :vali…

…date => false is used. [#486 state:resolved]

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

0 comments on commit b2b7611

Please sign in to comment.
Something went wrong with that request. Please try again.