Polymorphic has_and_belongs_to_many association

What you are describing cannot be accomplished with a single query. The issue is that a polymorphic association requires that you query the db to determine the class of the model, and you need that information to know what table to query to get the record you want. Some SQL dbs provide some kind of recursive query that might be able to do what you want in one query, but it would be pretty complicated and I can’t guess how difficult it would be to manage the performance of those queries. That doesn’t seem like something for core. If you want many-to-many association with polymorphism in one query, one way is to use has_many :through with an STI associated model.

Actually, since we are only storing the model name in the DB, it simply cannot be done at all, as the model name might be different from the table name.