method
add_multiple_associated_save_callbacks

add_multiple_associated_save_callbacks(association_name)
private
Hide source
# File activerecord/lib/active_record/associations.rb, line 986 def add_multiple_associated_save_callbacks(association_name) method_name = "validate_associated_records_for_#{association_name}".to_sym define_method(method_name) do association = instance_variable_get("@#{association_name}") if association.respond_to?(:loaded?) if new_record? association else association.select { |record| record.new_record? } end.each do |record| errors.add "#{association_name}" unless record.valid? end end end validate method_name before_save("@new_record_before_save = new_record?; true") after_callback = "association = instance_variable_get(\"@\#{association_name}\")\n\nrecords_to_save = if @new_record_before_save\nassociation\nelsif association.respond_to?(:loaded?) && association.loaded?\nassociation.select { |record| record.new_record? }\nelse\n[]\nend\n\nrecords_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank?\n\n# reconstruct the SQL queries now that we know the owner's id\nassociation.send(:construct_sql) if association.respond_to?(:construct_sql)\n" # Doesn't use after_save as that would save associations added in after_create/after_update twice after_create(after_callback) after_update(after_callback) end