I need to implement very simple messaging system between 2 users, and the only requirement is to keep every conversation between 2 users separate.
I wonder whether it's possible and reasonable to use only one model Message(sender_id, recipient_id )
to achieve the goal.
Suppose, I want to get index of all conversations.
- Join on
user.id = recipient_id
anduser.id = sender_id
to get all messages associated with specific user - Order results by
id
of opposed participants(whether it issender_id
orrecipient_id
) to form groups of messages - chatboxes. This is I think the most difficult step, since opposed user might be a sender or a receiver in one conversation. - Further order results inside each group by date of creation to show messages in their original order. Each group denotes a different conversation.
I tried Mailboxer
gem, but it seems to be overkill for this case.
I also think of a different solution based on introduction of new Conversation
entity with one-to-many relation with messages.