title: Rails Many-to-many relationship with extension generating incorrect SQL
body: <p>I'm having an issue where a many-to-many relationship with an "extension" is generating incorrect SQL.</p>
  
  <pre><code>class OrderItem &lt; ApplicationRecord
    belongs_to :buyer, class_name: :User
    belongs_to :order
    belongs_to :item, polymorphic: true
  end
  
  class User &lt; ApplicationRecord
    has_many :order_items_bought,
      -&gt; { joins(:order).where.not(orders: { state: :expired }).order(created_at: :desc) },
      foreign_key: :buyer_id,
      class_name: :OrderItem
  
    has_many :videos_bought,
      -&gt; { joins(:orders).select('DISTINCT ON (videos.id) videos.*').reorder('videos.id DESC') },
      through: :order_items_bought,
      source: :item,
      source_type: :Video do
        def confirmed
          where(orders: { state: :confirmed })
        end
      end
  end
  </code></pre>
  
  <p><code>user.videos_bought.confirmed</code> generates this SQL:</p>
  
  <pre><code>Video Load (47.0ms)  SELECT  DISTINCT ON (videos.id) videos.* FROM 
  "videos" INNER JOIN "order_items" "order_items_videos_join" ON 
  "order_items_videos_join"."item_id" = "videos"."id" AND 
  "order_items_videos_join"."item_type" = $1 INNER JOIN 
  "orders" ON "orders"."id" = "order_items_videos_join"."order_id" INNER JOIN 
  "order_items" ON "videos"."id" = "order_items"."item_id" WHERE 
  "order_items"."buyer_id" = $2 AND ("orders"."state" != $3) AND "order_items"."item_type" = $4 AND 
  "orders"."state" = $5 ORDER BY videos.id DESC, "order_items"."created_at" DESC LIMIT $6
  </code></pre>
  
  <p>Which returns some <code>Video</code> records which are joined with orders that do NOT have state confirmed. I would expect all orders to have state confirmed.</p>
  
  <p>If I use raw SQL everything works fine:</p>
  
  <pre><code>has_many :videos_bought,
    -&gt; {
      joins('INNER JOIN orders ON orders.id = order_items.order_id')
        .select('DISTINCT ON (videos.id) videos.*')
        .reorder('videos.id DESC')
    },
    through: :order_items_bought,
    source: :item,
    source_type: :Video do
      def confirmed
        where(orders: { state: :confirmed })
      end
  end
  </code></pre>
  
  <p>Now <code>user.videos_bought.confirmed</code> generates this SQL:</p>
  
  <pre><code>Video Load (5.4ms)  SELECT  DISTINCT ON (videos.id) videos.* FROM 
  "videos" INNER JOIN "order_items" ON 
  "videos"."id" = "order_items"."item_id" INNER JOIN orders ON 
  orders.id = order_items.order_id WHERE 
  "order_items"."buyer_id" = $1 AND ("orders"."state" != $2) AND
  "order_items"."item_type" = $3 AND "orders"."state" = $4 ORDER BY
  videos.id DESC, "order_items"."created_at" DESC LIMIT $5
  </code></pre>
  
  <p>Which seems more succinct because it avoids the auto generated <code>order_items_videos_join</code> name. It also only returns orders that have state confirmed.</p>
  
  <p>Any idea what is going on? Does ActiveRecord just generate faulty SQL sometimes?</p>
  
  <p>Using rails <code>5.1.5</code>. Upgrading to latest made no difference.</p>
  
  <p>I'm hoping to get an explanation on why Rails generates the <code>order_items_videos_join</code> string in the first case but not in the second case. Also, why the second SQL query produces incorrect results. I can edit the question with more code and data samples if needed.</p>