lib/active_scaffold/helpers/controller_helpers.rb
# build an associated record for association
build an associated record for association
build an associated record for association
def build_associated(association, parent_record)
        if association.through? && association.through_reflection.collection?
          parent_record = build_associated(association.class.new(association.through_reflection), parent_record)
          source_assoc = association.class.new(association.source_reflection)
          build_associated(source_assoc, parent_record).tap do |record|
            save_record_to_association(record, source_assoc.reverse_association, parent_record) 
          end
        elsif association.through? 
          parent_record = parent_record.send(association.through_reflection.name)
          source_assoc = association.class.new(association.source_reflection)
          build_associated(source_assoc, parent_record)
        elsif association.collection?
          parent_record.send(association.name).build
        elsif association.belongs_to? || parent_record.new_record? || parent_record.send(association.name).nil?
          parent_record.send("build_#{association.name}")
        else
          association.klass.new.tap do |record|
            save_record_to_association(record, association.reverse_association, parent_record) 
          end
        end
      end
def build_associated ( association , parent_record ) if association . through? && association . through_reflection . collection? parent_record = build_associated ( association . class . new ( association . through_reflection ) , parent_record ) source_assoc = association . class . new ( association . source_reflection ) build_associated ( source_assoc , parent_record ) . tap do | record | save_record_to_association ( record , source_assoc . reverse_association , parent_record ) end elsif association . through? parent_record = parent_record . send ( association . through_reflection . name ) source_assoc = association . class . new ( association . source_reflection ) build_associated ( source_assoc , parent_record ) elsif association . collection? parent_record . send ( association . name ) . build elsif association . belongs_to? || parent_record . new_record? || parent_record . send ( association . name ) . nil? parent_record . send ( "build_#{association.name}" ) else association . klass . new . tap do | record | save_record_to_association ( record , association . reverse_association , parent_record ) end end end
build an associated record for association
build an associated record for association
# build full chain, only check create_associated on initial parent_record # set inverse # through belongs_to/has_one # avoid use build_association in has_one when record is saved and had associated record # because associated record would be changed in DB # set inverse
association
<not_specific>
parent_record
<not_specific>