method

add_multiple_associated_save_callbacks

Importance_0
v2.0.3 - 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 1093
        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
          before_save("@new_record_before_save = new_record?; true")

          after_callback = "association = instance_variable_get(\"\#{ivar}\") if instance_variable_defined?(\"\#{ivar}\")\n\nrecords_to_save = if @new_record_before_save\nassociation\nelsif association.respond_to?(:loaded?) && association.loaded?\nassociation.select { |record| record.new_record? }\nelsif association.respond_to?(:loaded?) && !association.loaded?\nassociation.target.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
Register or log in to add new notes.