pere slop
pere slop

Reputation: 1

Find conversations between users

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

Upvotes: 0

Views: 94

Answers (1)

Vane Trajkov
Vane Trajkov

Reputation: 768

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, ...]})

Upvotes: 1

Related Questions