Okay admittedly that title wasn't the best, but I don't know how else to ask this...
I'm using the Sequel gem with Postgres. I have a database with Playlists and Tracks, and I've filtered the Tracks by a specific attribute (so, I have a subset of the tracks).
I'm then using Playlists.where(tracks: filtered_tracks)
to filter the playlists down to the ones that contain the subset of tracks.
However, is it possible to only select playlists that have two or more Tracks from the subset of tracks? Say, something like Playlists.where(tracks: filtered_tracks, at_least: 2)
.
Some pointers to a final answer...
I assume your tables are defined in a way similar to this one:
And that we populate them with some data:
Using SQL, and assuming your filtered_tracks would be 'track a', 'track b' and 'track x' you get the answer you want by executing the following query:
How or whether this SQL statement can be "back-translated" to Sequel I am not sure. I really don't know how to add a SELECT within a WHERE using Sequel. However, you can always take advantage of the fact that, if necessary, Sequel will let you use custom SQL directly, which means you could actually write your SQL statement and execute it.