I have these models
class User < ActiveRecord::Base
has_and_belongs_to_many :chats
end
and
class Chat < ActiveRecord::Base
has_and_belongs_to_many :users
end
Which is a many_to_many relationship and what I want is to recover the only possible chat that exists with 2 users
eg.
me = current_user
user_to_chat = User.find(any_user_except_me)
chat = Chat.new
me.chats.each do |current_chat|
if current_chat.users.include?(user_to_chat)
chat = current_chat
break
end
end
What is the better way to do this query?
thx!