Search code examples
ruby-on-railsrubypolymorphismruby-on-rails-5.2

Rails 5.2 Polymorphic Association with polymorphic conditional


MODELS:

class Car < ApplicationRecord
  belongs_to :user

  has_one  :listing, as: :listable
  has_one  :firm, as: :firmable
  has_one  :seller, as: :sellable
end

class Truck < ApplicationRecord
  belongs_to :user

  has_one  :listing, as: :listable
  has_one  :firm, as: :firmable
  has_one  :seller, as: :sellable
end

class Listing < ApplicationRecord
  belongs_to :listable, polymorphic: true
  has_many :favorites, dependent: :destroy
  has_many :users_who_favorited, through: :favorites, source: :user
end

And Assume that Car and Truck both have a user_id field....

Listing.includes(:listable) returns an eargerloaded listings AR relation.

However, I need to filter by the user_id, so I tried...

Listing.includes(:listable).where(user_id: 100)

but I get an error..

ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column listings.user_id does not exist) LINE 1: SELECT "listings".* FROM "listings" WHERE "listings"."user_...

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.

I also tried:

Listing.includes(listable:[:user]).where('users.id = 100')

but I get...

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

UPDATE:

Then tried

class Listing < ApplicationRecord
  belongs_to :listable, polymorphic: true
  has_many :favorites, dependent: :destroy
  has_many :users_who_favorited, through: :favorites, source: :user

  belongs_to :car, -> { includes(:listable).where(listable: { listable_type: Car.to_s }) }, foreign_key: :listable_id
  belongs_to :truck, -> { includes(:listable).where(listable: { listable_type: Truck.to_s }) }, foreign_key: :listable_id

end

and tried Listing.includes(:car, :truck) but got..

ActiveRecord::ConfigurationError (Can't join 'Car' to association named 'listable'; perhaps you misspelled it?)

Therefore, I can not try the below until the above is working.

Listing.includes(:car, :truck).where(cars: { user_id: 1 }).or(Listing.includes(:car, :truck).where(trucks: { user_id: 1 }))

However, I can do Listing.includes(:listable) and it does work, it breaks when I add a conditional.


Solution

  • This is a very interesting question that I had for several months. Then I found a solution for it.

    In your Listing model, in order to be able to include your polymorphic model, you'll need to tell your model that they are related.

    class Car < ApplicationRecord
      belongs_to :user
    
      has_one  :listing, as: :listable
      has_one  :firm, as: :firmable
      has_one  :seller, as: :sellable
    end
    
    class Truck < ApplicationRecord
      belongs_to :user
    
      has_one  :listing, as: :listable
      has_one  :firm, as: :firmable
      has_one  :seller, as: :sellable
    end
    
    class Listing < ApplicationRecord
      belongs_to :listable, polymorphic: true
      has_many :favorites, dependent: :destroy
      has_many :users, through: :favorites
    
      #magic happens here
      belongs_to :car, -> { includes(:listings).where(listings: { listable_type: Car.to_s }) }, foreign_key: :listable_id
      belongs_to :truck, -> { includes(:listings).where(listings: { listable_type: Truck.to_s }) }, foreign_key: :listable_id
    
    end
    

    and now, you can simply do: Listing.includes(:car, :truck) and it will work perfectly :-)

    For your case:

    Listing.includes(:car, :truck).where(cars: { user_id: 1 }).or(Listing.includes(:car, :truck).where(trucks: { user_id: 1 }))