Find conversation Between Specific Set of User ID's - Mailboxer Rails -


the title of question says all, wondering if can me find conversation exists between specific set of user id's. basically, if tries start conversation set of users, if conversation exact set of users exists, want app load conversation rather create one. proper query this? tried looking @ conversation class: http://rubydoc.info/gems/mailboxer/conversation. when run

conversation.first.participants 

i can see participants in conversation, didn't see way query conversation contains set of id's.

what want able like:

find conversation participants [current_user.id, 3, 5] 

and match conversation participants id's [current_user.id, 3, 5] not conversation participants id's [current_user.id, 3] or conversation participants id's [current_user.id, 3, 5, 7].

after referencing mailboxer coversation doc , sql finding records have same relationships , rails query doc

i think should work

participants_id = [current_user.id, 3, 5] select('distinct mailboxer_conversations.*').   where('mailboxer_notifications.type'=> mailboxer::message.name).   order("mailboxer_conversations.updated_at desc").   joins(:receipts).where('mailboxer_receipts.receiver_id in (#{participants_id.join(",")}) , mailboxer_receipts.receiver_type => #{current_user.class.base_class.to_s})')').group('mailboxer_conversations.id').having('count(1) = #{participants_id.length}') 

the first part of query

participants_id = [current_user.id, 3, 5] select('distinct mailboxer_conversations.*').   where('mailboxer_notifications.type'=> mailboxer::message.name).   order("mailboxer_conversations.updated_at desc").   joins(:receipts).where('mailboxer_receipts.receiver_id in (#{participants_id.join(",")} , mailboxer_receipts.receiver_type => #{current_user.class.base_class.to_s})') 

is used mailboxer if user participant or not

the second part

.group('mailboxer_conversations.id').having('count(1) = #{participants_id.length}') 

is used group results conversation id , filters results not repeated each participant means return 1 has relation of them.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -