What is the easiest and fastest way to achieve a clause where all elements in an array must be matched - not only one when using IN
? After all it should behave like mongodb's $all.
Thinking about group conversations where conversation_users is a join table between conversation_id and user_id I have something like this in mind:
WHERE (conversations_users.user_id ALL IN (1,2))
UPDATE 16.07.12
Adding more info about schema and case:
The join-table is rather simple:
Table "public.conversations_users" Column | Type | Modifiers | Storage | Description -----------------+---------+-----------+---------+------------- conversation_id | integer | | plain | user_id | integer | | plain |
A conversation has many users and a user belongs to many conversations. In order to find all users in a conversation I am using this join table.
In the end I am trying to figure out a ruby on rails
scope
that find's me a conversation depending on it's participants - e.g.:scope :between, ->(*users) { joins(:users).where('conversations_users.user_id all in (?)', users.map(&:id)) }
UPDATE 23.07.12
My question is about finding an exact match of people. Therefore:
Conversation between (1,2,3)
won't match if querying for (1,2)
(1,2)
, do you also want one between(1,2,3)
in the result, or only conversations between (1,2) - and nobody else? – Nembutal