title: Rails 5.2 Polymorphic Association with polymorphic conditional
body: <h3>MODELS:</h3>
  
  <pre><code>class Car &lt; ApplicationRecord
    belongs_to :user
  
    has_one  :listing, as: :listable
    has_one  :firm, as: :firmable
    has_one  :seller, as: :sellable
  end
  
  class Truck &lt; ApplicationRecord
    belongs_to :user
  
    has_one  :listing, as: :listable
    has_one  :firm, as: :firmable
    has_one  :seller, as: :sellable
  end
  
  class Listing &lt; ApplicationRecord
    belongs_to :listable, polymorphic: true
    has_many :favorites, dependent: :destroy
    has_many :users_who_favorited, through: :favorites, source: :user
  end
  </code></pre>
  
  <p>And Assume that Car and Truck both have a user_id field....</p>
  
  <p><code>Listing.includes(:listable)</code> returns an eargerloaded listings AR relation.</p>
  
  <p>However, I need to filter by the user_id, so I tried...</p>
  
  <p><code>Listing.includes(:listable).where(user_id: 100)</code></p>
  
  <p>but I get an error..</p>
  
  <blockquote>
    <p>ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR:  column listings.user_id does not exist)
        LINE 1: SELECT  "listings".* FROM "listings" WHERE "listings"."user_...</p>
  </blockquote>
  
  <p>As it seems to be looking up user_id on listings. However, I need to filter on listables table, so that would mean either the Car or Truck tables. However listable is defined.</p>
  
  <p>I also tried:</p>
  
  <p><code>Listing.includes(listable:[:user]).where('users.id = 100')</code></p>
  
  <p>but I get...</p>
  
  <blockquote>
    <p>ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "users")
        LINE 1: SELECT  "listings".* FROM "listings" WHERE (users.id = 100) ...
                                                            ^
        : SELECT  "listings".* FROM "listings" WHERE (users.id = 100) LIMIT $1</p>
  </blockquote>
  
  <h3>UPDATE:</h3>
  
  <p>Then tried</p>
  
  <pre><code>class Listing &lt; ApplicationRecord
    belongs_to :listable, polymorphic: true
    has_many :favorites, dependent: :destroy
    has_many :users_who_favorited, through: :favorites, source: :user
  
    belongs_to :car, -&gt; { includes(:listable).where(listable: { listable_type: Car.to_s }) }, foreign_key: :listable_id
    belongs_to :truck, -&gt; { includes(:listable).where(listable: { listable_type: Truck.to_s }) }, foreign_key: :listable_id
  
  end
  </code></pre>
  
  <p>and tried <code>Listing.includes(:car, :truck)</code> but got..</p>
  
  <blockquote>
    <p>ActiveRecord::ConfigurationError (Can't join 'Car' to association named 'listable'; perhaps you misspelled it?)</p>
  </blockquote>
  
  <p>Therefore, I can not try the below until the above is working.</p>
  
  <p><code>Listing.includes(:car, :truck).where(cars: { user_id: 1 }).or(Listing.includes(:car, :truck).where(trucks: { user_id: 1 }))</code></p>
  
  <p>However, I can do Listing.includes(:listable) and it does work, it breaks when I add a conditional.</p>