Listing Model
belongs_to :listable, polymorphic: true
belongs_to :car, -> { where(listings: {listable_type: 'Car'}) }, foreign_key: 'listable_id'
def car
return unless listable_type == "Car"
super
end
listing = Listing.first
listing.car
Error
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "listings") LINE 1: ....* FROM "cars" WHERE "cars"."id" = $1 AND "listings"... ^ : SELECT "cars".* FROM "cars" WHERE "cars"."id" = $1 AND "listings"."listable_type" = $2 LIMIT $3
I also tried:
belongs_to :car, -> { joins(:listings).where(listings: {listable_type: 'Car'}) }, foreign_key: 'listable_id'
and I got:
ActiveRecord::ConfigurationError (Can't join 'Car' to association named 'listings'; perhaps you misspelled it?)
What is causing this error?