title: Rails Nested Joins Activerecord with conditions
body: <p>I'm trying to write a nested joins query with a condition.</p>
  
  <p>The query I have right now is:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>Event.joins(:store =&gt; :retailer).where(store: {retailer: {id: 2}})
  </code></pre>
  
  <p>Which outputs the following SQL:</p>
  
  <pre><code>   SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
  :id: 2
  '
  </code></pre>
  
  <p>And also the following error:</p>
  
  <pre><code>SQLite3::SQLException: no such column: store.retailer_id: SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
  :id: 2
  '
  </code></pre>
  
  <p>It's telling me there is no column store.retailer_id, however, I can run the following query and it will work just fine:</p>
  
  <pre><code>Event.first.store.retailer_id
    Event Load (0.2ms)  SELECT  "events".* FROM "events"   ORDER BY "events"."id" ASC LIMIT 1
    Store Load (0.1ms)  SELECT  "stores".* FROM "stores"  WHERE "stores"."id" = ? LIMIT 1  [["id", 28958]]
  =&gt; 4
  </code></pre>