I've got two models: Playlist
and Item
. I want to build a query which returns all playlists except those which have an specific item.
These are the models:
class Playlist < ApplicationRecord
has_many :items
end
class Item < ApplicationRecord
belongs_to :playlist
end
This is a query which doesn't work since there could be playlists which have the specified item, but also several more, and if they have other items, they are included (which is what I don't want):
Playlist.left_outer_joins(:items).where.not(items: { uid: id })
For the record, my DBMS is PostgreSQL 9.6