method
add_multiple_associated_save_callbacks
add_multiple_associated_save_callbacks(association_name)
private
Hide source
# File activerecord/lib/active_record/associations.rb, line 786 def add_multiple_associated_save_callbacks(association_name) method_name = "validate_associated_records_for_#{association_name}".to_sym define_method(method_name) do @new_record_before_save = new_record? 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 after_callback = "association = instance_variable_get(\"@\#{association_name}\")\nif association.respond_to?(:loaded?)\nif @new_record_before_save\nrecords_to_save = association\nelse\nrecords_to_save = association.select { |record| record.new_record? }\nend\nrecords_to_save.each { |record| association.send(:insert_record, record) }\nassociation.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id\nend\n\n@new_record_before_save = false\ntrue\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