Flowdock
method

add_multiple_associated_save_callbacks

Importance_0
Ruby on Rails latest stable (v4.0.2) - 0 notes - Class: ActiveRecord::Associations::ClassMethods

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.2.1) is shown here.

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.