title: Rails .joins query over multiple associations
body: <p>I have this query that works as expected:</p>
  
  <p><code>@dog.listings.joins(:address_country).merge(Country.where(permalink: 'uk'))</code></p>
  
  <p>This query gives me the Listings where the country matches 'uk' (Listing has_one :address_country, which is a country from the Country model)</p>
  
  <p>But when I add another association to the chain in between cat and listing (litter), it doesn't work (a litter belongs to a listing, as well as to a cat):</p>
  
  <p><code>@dog.litters.joins(:listing) &amp; Listing.joins(:address_country) &amp; Country.where(permalink: 'uk')</code></p>
  
  <p>In this query I'd like it to fetch the Litters where the country (of the associated listing) matches. But it just returns an empty array. The first query works, and I guess I just need to bolt that on to <code>@cat.litters</code>?)</p>
  
  <p>In Rails C, I'm getting this:</p>
  
  <pre><code> d.litters.joins(:listing) &amp; Listing.joins(:address_country).merge(Country.where(permalink: 'uk'))
  
    Litter Load (0.6ms)  SELECT "litters".* FROM "litters" INNER JOIN "listings" ON "listings"."id" = "litters"."listing_id" WHERE "litters"."litterable_id" = 11 AND "litters"."litterable_type" = 'Dog'
    Listing Load (0.4ms)  SELECT "listings".* FROM "listings" INNER JOIN "countries" ON "countries"."id" = "listings"."address_country_id" WHERE "countries"."permalink" = 'uk'
  =&gt; []
  </code></pre>
  
  <p>Any ideas what I'm doing wrong?</p>