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