method

add_multiple_associated_save_callbacks

Importance_0
v1.1.6 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
add_multiple_associated_save_callbacks(association_name) private

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/associations.rb, line 912
        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\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"
                
          # 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
Register or log in to add new notes.