Direct Message System Relationships

Hey everyone! I’m building a direct message system with elixir and phoenix and I can’t seem to workout how I should model the relationships.

I have 3 models: User, Conversation, Messages.

Users is a basic user type with e-mail, password, etc.

Messages have the content of the message and belong to a conversation.

Conversations keep track of both users involved in the conversation like so:
belongs_to :sender, App.User
belongs_to :recipient, App.User

I’m having a hard time figuring out how I can model this relationship for users. I’ve figured I can do 2 has_many relationships with the corresponding foreign keys of sender_id and recipient_id. I can then preload both associations; however, there must be a more efficient way to do it.

I’m new to phoenix so forgive me if there is an obvious solution! I’ve been scrambling through the Ecto docs for an answer.

Thanks!

What do you think is inefficient with your current approach?

There is a nice blog post on ecto self referential association here: