0
這是關係看怎麼樣用連接查詢產生未完成的結果?
class Conversation < ApplicationRecord
belongs_to :user
has_many :messages
class Message < ApplicationRecord
belongs_to :sender, class_name: 'User'
belongs_to :receiver, class_name: 'User'
belongs_to :conversation
我想所有對話並且用戶是所有者或接收或發送者,我做它像這樣
[79] pry(main)> Conversation.joins(:messages).where("messages.receiver_id = ? OR messages.sender_id = ? OR conversations.user_id = ?", 2, 2, 2).count
(0.5ms) SELECT COUNT(*) FROM "conversations" INNER JOIN "messages" ON "messages"."conversation_id" = "conversations"."id" WHERE (messages.receiver_id = 2 OR messages.sender_id = 2 OR conversations.user_id = 2)
=> 0
但0
是不正確的,我有對話user_id: 2
[78] pry(main)> Conversation.where(user_id: 2).count
(0.4ms) SELECT COUNT(*) FROM "conversations" WHERE "conversations"."user_id" = $1 [["user_id", 2]]
=> 1
任何想法什麼是錯我的查詢?