module ActiveRecord::AutosaveAssociation::ClassMethods

def add_autosave_association_callbacks(reflection)

before actually defining them.
check if the save or validation methods have already been defined
the callbacks to get defined multiple times, there are guards that
called _after_ the association has been defined. Since we don't want
this can change, for instance, when using nested attributes, which is
but instead only define the method and callback when needed. However,
For performance reasons, we don't check whether to validate at runtime,

the +reflection+.
Adds a validate and save callback for the association as specified by
def add_autosave_association_callbacks(reflection)
  save_method = :"autosave_associated_records_for_#{reflection.name}"
  validation_method = :"validate_associated_records_for_#{reflection.name}"
  collection = reflection.collection?
  unless method_defined?(save_method)
    if collection
      before_save :before_save_collection_association
      define_method(save_method) { save_collection_association(reflection) }
      # Doesn't use after_save as that would save associations added in after_create/after_update twice
      after_create save_method
      after_update save_method
    else
      if reflection.macro == :has_one
        define_method(save_method) { save_has_one_association(reflection) }
        after_save save_method
      else
        define_method(save_method) { save_belongs_to_association(reflection) }
        before_save save_method
      end
    end
  end
  if reflection.validate? && !method_defined?(validation_method)
    method = (collection ? :validate_collection_association : :validate_single_association)
    define_method(validation_method) { send(method, reflection) }
    validate validation_method
  end
end