reflection is not has_many :through; it's a standard has_many / belongs_to instead
 TODO: see if we can defer to rails code here a bit more
338243
def construct_has_many_or_belongs_to_attributes(reflection, association_class, table_ids)
      # Determine the alias used for remote_table_name, if any. In all cases this will already
      # have been assigned an ID in table_ids (either through being involved in a previous join,
      # or - if it's the first table in the query - as the default value of table_ids)
      remote_table_alias = remote_table_name = association_class.table_name
      remote_table_alias += "_#{table_ids[remote_table_name]}" unless table_ids[remote_table_name] == 1

      # Assign a new alias for the local table.
      local_table_alias = local_table_name = reflection.active_record.table_name
      if table_ids[local_table_name]
        table_id = table_ids[local_table_name] += 1
        local_table_alias += "_#{table_id}"
      else
        table_ids[local_table_name] = 1
      end

      conditions = ''
      # Add type_condition, if applicable
      conditions += " AND #{association_class.send(:type_condition).to_sql}" if association_class.finder_needs_type_condition?
      # Add custom conditions
      conditions += " AND (#{association_class.send(:sanitize_sql, reflection.options[:conditions])})" if reflection.options[:conditions]

      if reflection.macro == :belongs_to
        if reflection.options[:polymorphic]
          conditions += " AND #{local_table_alias}.#{reflection.options[:foreign_type]} = #{reflection.active_record.quote_value(association_class.base_class.name.to_s)}"
        end
        {
          :joins => reflection.options[:joins],
          :remote_key => "#{remote_table_alias}.#{association_class.primary_key}",
          :local_key => reflection.primary_key_name,
          :conditions => conditions
        }
      else
        # Association is has_many (without :through)
        if reflection.options[:as]
          conditions += " AND #{remote_table_alias}.#{reflection.options[:as]}_type = #{reflection.active_record.quote_value(reflection.active_record.base_class.name.to_s)}"
        end
        {
          :joins => "#{reflection.options[:joins]}",
          :remote_key => "#{remote_table_alias}.#{reflection.primary_key_name}",
          :local_key => reflection.klass.primary_key,
          :conditions => conditions
        }
      end
    end
485674
def associations
      @associations ||= Hash[metadata.xpath('//Association').collect do |association_definition|
        [
            association_definition.attributes['Name'].value,
            build_association(association_definition)
        ]
      end]
    end
73615
def has_many_elements(element, target_model)
      element.class.reflect_on_all_associations(:has_many).each do |assoc|
        if assoc.name.to_s == target_model.downcase.pluralize
          model = Object.const_get(target_model.singularize.camelize)
          return element.send(target_model.downcase.pluralize).paginate(:per_page => 10, :page => 1)
        end
      end
      return []
    end
747182
def association_valid?(reflection, record)
      return true if record.destroyed? || record.marked_for_destruction?

      unless valid = record.valid?
        if reflection.options[:autosave]
          record.errors.each do |attribute, message|
            attribute = "#{reflection.name}.#{attribute}"
            errors[attribute] << message
            errors[attribute].uniq!
          end
        else
          errors.add(reflection.name)
        end
      end
      valid
    end
914118
def reflected_names(obj)
      classes = obj.reflections
      class_names = []
      classes.each do |key, cl|
        if eval(cl.class_name).respond_to?("diffable") \
           and cl.association_class != ActiveRecord::Associations::BelongsToAssociation
          class_names << key
        end
      end
      class_names
    end
1161030
def method_missing(meth, *args, &block)
      raise NoMethodError, "undefined method #{meth} for #{self.class.to_s}" if WotApiRu::Constants::PATH[meth.to_sym].nil?
      options = args[0] || {}
      options[:application_id] ||= WotApiRu::Settings[:application_id]
      path = WotApiRu::Constants::HOST + WotApiRu::Constants::PATH[meth.to_sym]
      invoke(path, options)
    end
981125
def includes_reflections
      hash = []
      @table.columns.each do |column|
        hash << column.reflection.name if column.respond_to?(:reflection)
      end
      hash
    end
1017330
def map_reflection_for( path )
      raise "reflection for #{path.inspect} already exists" unless reflections[path].nil?

      reflection = path.empty? ? top_level_model : begin
        parent = reflection_for( path[0..-2] )
        if !Authorization.is_a_association_proxy?(parent) and parent.respond_to?(:klass)
          parent.klass.reflect_on_association( path.last )
        else
          parent.reflect_on_association( path.last )
        end
      rescue
        parent.reflect_on_association( path.last )
      end
      raise "invalid path #{path.inspect}" if reflection.nil?

      reflections[path] = reflection
      map_table_alias_for( path )  # Claim a table alias for the path.

      # Claim alias for join table
      # TODO change how this is checked
      if !Authorization.is_a_association_proxy?(reflection) and !reflection.respond_to?(:proxy_scope) and reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
        join_table_path = path[0..-2] + [reflection.options[:through]]
        reflection_for(join_table_path, true)
      end
      
      reflection
    end
499634
def create
      # TODO: use the has_many associations -> @app.reviews.build
      @app_review = review_klass.new(review_params)
      if @app_review.save
        after_save
        render :show
      else
        render json: @app_review.errors, status: :bad_request
      end
    end
966171
def errors
      found_errors = @record.all('[data-error-for]')
      error_list = ActiveModel::Errors.new(self)
      found_errors.each do | error |
        attribute = error['data-error-for']
        message = error.text
        error_list.add(attribute, message)
      end
      error_list
    end
723834
def hack_for_has_many_counter_cache(parent_record, column, value)
      association = parent_record.association(column.name)
      counter_attr = association.send(:cached_counter_attribute_name)
      difference = value.select(&:persisted?).size - parent_record.send(counter_attr)

      if parent_record.new_record?
        parent_record.send "#{column.name}=", value
        parent_record.send "#{counter_attr}_will_change!"
      else
        # don't decrement counter for deleted records, on destroy they will update counter
        difference += (parent_record.send(column.name) - value).size
        association.send :update_counter, difference unless difference.zero?
      end

      # update counters on old parents if belongs_to is changed
      value.select(&:persisted?).each do |record|
        key = record.send(column.association.foreign_key)
        parent_record.class.decrement_counter counter_attr, key if key && key != parent_record.id # rubocop:disable Rails/SkipsModelValidations
      end
      parent_record.send "#{column.name}=", value if parent_record.persisted?
    end
1081592
def method_missing(sym, *args, &block)
      if (data && data[sym])
        data[sym]
      else
        super(sym, *args, &block)
      end
    end
846125
def method_missing(name, *args)
      value = @values[name]
      if value.nil? && !@values.has_key?(name)
        address = "<#{self.class.name}:0x00#{(self.object_id << 1).to_s(16)}>"
        raise NoMethodError.new("undefined method `#{name}' for ##{address}")
      end
      value
    end
1276360
def create_or_find_by!(attributes, &block)
      transaction(requires_new: true) { create!(attributes, &block) }
    rescue ActiveRecord::RecordNotUnique
      find_by!(attributes)
    end
1158105
def update_cloneable_associations
      associations_for_cloning.each do |association|
        type = @klass.reflect_on_association(association)
        through_record = type.through_reflection

        if through_record.present?
          clone_join_relationships(through_record.plural_name)
        else
          clone_has_one_relationship(association,type) if type.macro == :has_one
          clone_has_many_relationships(association) if type.macro == :has_many
        end
      end
    end
486160
def dependent_association_scopes
      self.class.reflect_on_all_associations.select { |reflection|
        reflection.options[:dependent] == :destroy and reflection.klass.paranoid?
      }.map { |reflection|
        self.association(reflection.name).tap {|assoc| assoc.reset_scope }.scope
      }
    end
672076
def check_validity(reflection)
        if reflection.association_foreign_key == reflection.foreign_key
          raise ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded.new(reflection)
        end

        reflection.options[:join_table] ||= join_table_name(
          model.send(:undecorated_table_name, model.to_s),
          model.send(:undecorated_table_name, reflection.class_name)
        )
      end