Search code examples
rubysequel

Matching list of values on multiple columns in Sequel


Given a table with a composite primary key, say a and b. If I have a list of pairs that I would like to query for, say,

pairs = [[1, 1], [2, 2], [2, 1]]

How can I filter using these in a dataset?

The result in SQL would look like

WHERE (a, b) IN ((1, 1), (2, 2), (2, 1))

There's DB.values() that can be used to express the right side, but I'm not sure how to get the actual filtering into a dataset.


Solution

  • DB[:table].where([:a, :b]=>[[1,1], [2,2], [2,1]])
    # SELECT * FROM table WHERE ((a, b) IN ((1, 1), (2, 2), (2, 1)))