# File lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb, line 7
    def build(associations, base_klass)
      associations.map do |name, right|
        if name.is_a? Join
          reflection = find_reflection base_klass, name.name
          reflection.check_validity!
          reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5

          klass = if reflection.polymorphic?
                    name.klass || base_klass
                  else
                    reflection.klass
                  end
          JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
        else
          reflection = find_reflection base_klass, name
          reflection.check_validity!
          reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5

          if reflection.polymorphic?
            raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
          end
          JoinAssociation.new reflection, build(right, reflection.klass)
        end
      end
    end