I would like to use squeel in order to build query like:
SELECT * from `table`
WHERE (`field1`, `field2`)
NOT IN (
(1,"value_a"),
(2,"value_b"),
(3,"value_a"),
...
)
I want to know if there is any way to compare multiple fields with an array via IN
or NOT IN
statement.
Something like this (I am aware that example does not work) would be a nice way to express what I mean:
array = [[1,"value_a"], [2,"value_b"], [3, "value_a"]]
Table.where{ (field1 & filed2).not_in array }
Is something like this possible at all?
I know how to get the same final result using multiple ... & (a != b) & (c != d) & ...
, but that was not what I have asked.
Main problem is that, while Arel (and with that Squeel) supports some fairly complex queries, this level of query complexity is not covered. With that in mind, reverting back to SQL and manually constructing needed query is the only solution other than extending Arel itself with this functionality.
existing_compound_ids = [[1,"value_a"],[2,"value_b"],[3, "value_a"]]
compound_collection = existing_compound_ids.inject([]) do |aftermath, member|
aftermath << "('#{ member[0] }','#{ member[1] }')"
end.join(",")
Table.where("(`tables`.`field1`, `tables`.field2) NOT IN (#{compound_collection})")
Will construct following SQL:
SELECT `tables`.* from `tables` WHERE ((field1, field2)
NOT IN (('1','value_a'),('2','value_b'),('3','value_a')))
I would love to see better, or more elegant, solution, but I have not found one yet.