0

これらは私のクラスと関係です

class User
  has_many :conversation_participants
  has_many :conversations, :through => :conversation_participants
end

class ConversationParticipant
  belongs_to :user
  belongs_to :conversation
end

class Conversation
  has_many :messages
  has_many :conversation_participants
  has_many :users, :through => :conversation_participants
end

したがって、user_ids 12と15の間の会話を作成する場合は、最初に、これら2つの間の会話がすでに存在するかどうかを確認します。だから私が見つける必要があるのはこれです:

user_id IN(12、15)および「両方のconversationparticipant行が同じ会話IDを持っている」ConversationParticipants

私はこの質問を説明するための適切な言葉が不足していますが、私が言っていることは誰もが理解できると思います。「この2人のユーザー間の会話はすでに存在しますか?」SQLでもRailsでもそれを行う方法がわからないので、どんな答えでもありがたいです。

編集 会話のIDはわかりません。これらの2つのuser_idの間に会話が存在するかどうかを確認する必要があります。

ありがとう-エミル

4

2 に答える 2

1
class User
  has_many :conversations
  has_many :conversation_participants
  has_many :joined_conversations, :through => :conversation_participants, :source => :conversation
end

class ConversationParticipant
  belongs_to :user
  belongs_to :conversation
end

class Conversation
  belongs_to :user
  has_many :conversation_participants
  has_many :speakers, :through => :conversation_participants, :source => :user
end
于 2012-04-16T12:15:36.017 に答える
0

これを試すことができます

 @result = ConversationParticipants.where("user_id in ? AND conversation_id=?",[1,2,3,4],2)
于 2012-04-16T12:14:16.470 に答える