Define a one to many association. Expect a name and a block to
 define the fields on associated records.

 For example:

   define_csv_fields do |c|
     has_many 'reviews' do |r|
       r.field :id, :name, :_destroy
     end
   end
979409
def has_many(name)
      @config.root or raise NotImplementedError, "nested associations are not supported"
      @config.has_manys[name.to_sym] ||= Config.new
      yield self.class.new config: @config.has_manys[name.to_sym], root: false

      nil
    end
657355
def many?
      if block_given?
        to_a.many? { |*block_args| yield(*block_args) }
      else
        limit_value ? to_a.many? : size > 1
      end
    end
587521
def associations_hash
      hash = {}
      @scope.associations.each do |association, options|
        hash.merge!(render_association(association, options))
      end
      hash
    end
920520
def validate_collection_association(reflection)
        if association = association_instance_get(reflection.name)
          if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
            records.each_with_index { |record, index| association_valid?(reflection, record, index) }
          end
        end
      end
763342
def monitor_association_changes(association)
      define_method "#{association}=" do |value|
        attribute_will_change!(association.to_s) if _association_will_change?(association, value)
        super(value)
      end

      ids = "#{association.to_s.singularize}_ids"

      define_method "#{ids}=" do |value|
        attribute_will_change!(association.to_s) if _ids_will_change?(ids, value)
        super(value)
      end

      define_method "#{association}_attributes=" do |value|
        attribute_will_change!(association.to_s) if _nested_attributes_will_change?(value)
        super(value)
      end

      [association, ids].each do |name|
        define_method "#{name}_change" do
          changes[name]
        end

        define_method "#{name}_changed?" do
          changes.has_key?(association.to_s)
        end

        define_method "#{name}_previously_changed?" do
          previous_changes.has_key?(association.to_s)
        end
      end
    end
650176
def each_association( objectpath )
    begin
      @client.associators(objectpath).each do |assoc|
        yield assoc
      end
    rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
    end
  end
212277
def child_relationships
      child_relationships = []

      relationships.each do |relationship|
        next unless relationship.respond_to?(:collection_for)
        set_default_value(relationship)
        next unless relationship.loaded?(self)

        child_relationships << relationship
      end

      many_to_many, other = child_relationships.partition do |relationship|
        relationship.kind_of?(Associations::ManyToMany::Relationship)
      end

      many_to_many + other
    end
1003483
def iterateUnionRecords(tables,fieldNames)
   
      raise "'iterateUnionRecords' must be called with a block." if not block_given?   
   
      if tables and tables.is_a?(Array)
         if fieldNames and fieldNames.is_a?(Array)
            tableRecords = []
            tables.each{|table|
               if table and table.is_a?(Hash) and table["dbid"]
                  tableRecords << getAllValuesForFields( table["dbid"],
                                                                        fieldNames,
                                                                        table["query"],
                                                                        table["qid"],
                                                                        table["qname"],
                                                                        table["clist"],
                                                                        table["slist"],
                                                                        "structured",
                                                                        table["options"])
               else
                  raise "'table' must be a Hash that includes an entry for 'dbid'."
               end
            }
         else
            raise "'fieldNames' must be an Array of field names valid in all the tables."
         end
      else
         raise "'tables' must be an Array of Hashes."
      end
      usedRecords = {}
      tableRecords.each{|queryResults|
         if queryResults
            numRecords = 0
            numRecords = queryResults[fieldNames[0]].length if queryResults[fieldNames[0]]
            (0..(numRecords-1)).each{|recNum|
               recordHash = {}
               fieldNames.each{|fieldName|
                  recordHash[fieldName]=queryResults[fieldName][recNum]
               }
               if not usedRecords[recordHash.values.join]
                  usedRecords[recordHash.values.join]=true
                  yield recordHash
               end
            }
         end
      }
   end
529131
def association_valid?(reflection, record, index = nil)
        return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)

        context = validation_context unless [:create, :update].include?(validation_context)

        unless valid = record.valid?(context)
          if reflection.options[:autosave]
            indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord::Base.index_nested_attribute_errors)

            record.errors.group_by_attribute.each { |attribute, errors|
              attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)

              errors.each { |error|
                self.errors.import(
                  error,
                  attribute: attribute
                )
              }
            }
          else
            errors.add(reflection.name)
          end
        end
        valid
      end
1366131
def attributes
      fields.each_with_object(HashWithIndifferentAccess.new) do |field, acc|
        acc[field.alias] = field.value
      end
    end
913287
def reflect_on_all_associations klass
        klass.relationships.collect do |relationship|
          # All can be child/parent
          # ManyToMany
          # ManyToOne
          # OneToMany
          # OneToOne
          #TODO :has_and_belongs_to_many
          if relationship.kind_of?(::DataMapper::Associations::ManyToOne::Relationship) && relationship.child_model? #relationship.options[:min] == 1 && relationship.options[:max] == 1
            macro = :belongs_to
            if relationship.options[:class_name]
              # In a belongs_to, the side with the class name uses
              # the parent model, but child key for the foreign key...
              class_name = relationship.parent_model.to_s
              primary_key_name = relationship.child_key.entries.first.name
            else
              class_name = relationship.child_model.to_s
              primary_key_name = relationship.parent_key.entries.first.name
            end
          else
            macro = :has_one
            if relationship.options[:class_name]
              # but on the has_one side, it's the *child* model that
              # uses the child key for the foreign key.  Weirdness.
              class_name = relationship.child_model.to_s
              primary_key_name = relationship.child_key.entries.first.name
            else
              class_name = relationship.parent_model.to_s
              primary_key_name = relationship.parent_key.entries.first.name
            end
          end
          OpenStruct.new(
            :name => relationship.name,
            :options => {
              #:foreign_key => , # Ignore, let the calling code infer: {name}_id
              :polymorphic => false # Hard code for now
            },
            :class_name => class_name,
            :primary_key_name => primary_key_name,
            :macro => macro
          )
        end
      end
371962
def ar_collection?(instance, method)
      defined?(ActiveRecord) &&
        instance.respond_to?(method) &&
        instance.send(method).is_a?(ActiveRecord::Relation)
    end
588596
def field_by_name(name)
      field = fields[name.to_sym] unless name.blank?
      if field.nil?
        dotted = name.to_s.split('.')[0]
        field = fields[dotted.to_sym] unless dotted.blank?
        if field && field.key_relation.nil?
          return nil
        end
      end
      field
    end
70634
def eachRecord( records = @records )
      if records and block_given?
         records.each { |record|
            if record.is_a?( REXML::Element) and record.name == "record"
               @record = record
               yield record
            end
         }
      end
      nil
   end
177897
def belongs_to(table, options = {})
      options = options.merge(:references => table)
      options[:on_delete] = options.delete(:dependent) if options.has_key?(:dependent)
      column("#{table.to_s.singularize}_id".to_sym, :integer, options)
    end
550018
def field_names(options)
      names = (as_attributes.keys + attribute_names).uniq.sort

      only = Array.wrap(options[:only]).map(&:to_s)
      except = Array.wrap(options[:except]).map(&:to_s)
      except |= ['_type'] unless Mongoid.include_type_for_serialization

      if !only.empty?
        names &= only
      elsif !except.empty?
        names -= except
      end
      names
    end
319526
def joins
      return nil if inner_joins.empty?
      inner_joins.collect do |inner_join|
        join_table_name = association_class(inner_join).table_name
        join_on_field = inner_join.last.to_s
        "INNER JOIN #{join_table_name} ON #{join_table_name}.id = #{table_name}.#{join_on_field}"
      end
    end