Find conversations between users

95 views Asked by At

I need an ActiveRecord query to find conversations for current_user and any another user

Conversations can have many users. I need to set users ids in that query(it can be two or more users).

class Conversation < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :conversations
end
1

There are 1 answers

3
Vane Trajkov On

You can join and put the query as a hash

Conversation.joins(:users).where(conversation_attr: some_value, users: { id:  current_user.id })

or

Conversation.joins(:users).where(conversation_attr: some_value, users: { id:  [user_id1, user_id2, ...]})