Flowdock
method

add_multiple_associated_save_callbacks

Importance_0
v2.2.1 - 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 1369
        def add_multiple_associated_save_callbacks(association_name)
          ivar = "@#{association_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
Register or log in to add new notes.