实现子句中最简单,最快速的方法是什么,必须匹配数组中的所有元素 - 使用时不仅仅是一个IN
?毕竟它应该像mongodb的$ all.
考虑到conversation_users是conversation_id和user_id之间的连接表的群组对话,我有类似这样的想法:
WHERE (conversations_users.user_id ALL IN (1,2))
Run Code Online (Sandbox Code Playgroud)
更新 16.07.12
添加有关架构和案例的更多信息:
join-table非常简单:
Table "public.conversations_users"
Column | Type | Modifiers | Storage | Description
-----------------+---------+-----------+---------+-------------
conversation_id | integer | | plain |
user_id | integer | | plain |
Run Code Online (Sandbox Code Playgroud)对话有很多用户,用户属于许多对话.为了找到对话中的所有用户,我正在使用此连接表.
最后,我试图在轨道scope
上找出一个红宝石,根据它的参与者找到我的对话 - 例如:
scope :between, ->(*users) {
joins(:users).where('conversations_users.user_id all in (?)', users.map(&:id))
}
Run Code Online (Sandbox Code Playgroud)更新 23.07.12
我的问题是找到一个完全匹配的人.因此:
(1,2,3)
如果查询,则之间的对话将不匹配(1,2)
sql postgresql activerecord ruby-on-rails relational-division