【发布时间】:2015-12-17 19:04:25
【问题描述】:
我有一个与
的对话模型recipient_id
sender_id
获取当前用户的对话看起来像
conversations = Conversation.where("conversations.sender_id =? OR conversations.recipient_id =?",current_user.id,current_user.id)
让我们调用 other_user 对话中不是 current_user 的用户。
由于当前用户既可以是发件人也可以是收件人,因此无法在 users 表上使用 JOIN 或 Include 来获取对话中涉及的“其他用户”。因此,呈现对话会产生 N+1 的情况,为每个其他用户发出 1 个查询。
有没有办法使用 include 或 JOIN 获取 other_user,条件基本上是:
if current_user_id == recipient_id JOIN on recipient_id
if current_user_id == sender_id JOIN on sender_id
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-4 join