0
votes

I am trying to make a messaging system in rails which will enable a user to send messages to a listing. Once they click on send message a new conversation is created between the user and and the listing. So a conversation is always between only 2 users.

here is basic setup

User has_many Listings

Listing belongs_to User

For adding private messaging system as described above I am planning like below

Conversation has_many Messages

Message belongs_to Conversation

listing has_many conversation

user has_many conversation

conversation belongs_to listing

conversation belongs_to user

I have never done something this crazy. I have a feeling that there is another better way to do this or am overkilling it. I know I easily create messaging if its between 2 users, but as a user can have many listings and I want a different conversation for each listing(so basically conversation is between a user and a listing) even its from the same owner. Could someone shed some light into which approach to go on schema design in this situation?

2

2 Answers

1
votes

I'd suggest using has_many through for conversations:


User has_many Listings

Listing belongs_to User

Conversation belongs_to Listing

Conversation has_many Messages

Message belongs_to Conversation

Listing has_many Conversations

UserA has_many conversations, through: listing, class: User

UserB has_many conversations, through: listing, class: User


It would be nice to think of better names of UserA and UserB of course, but sender/recipient doesn't really work.

1
votes

It looks fine, does it work?

One question: are conversations ever going to exist between users? If so you may want to do some predictive naming of your objects, e.g. ListingUserConversation.