# Need some help designing my threaded messaging system

**URL:** <https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637>\
**Category:** Rails\
**Created:** [January 21, 2006, 5:19am UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637 "2006-01-21T05:19:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [January 21, 2006, 5:19am UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637/1 "2006-01-21T05:19:14Z")

</div>

Hi,

I want to create a messaging system that recognizes threads of messages,  
not  
unlike gmail.

So far I have these models:

Conversation  
belongs\_to :user  
has\_many :messages

Message  
belongs\_to :conversation

The problem I am running into is not only does a conversation belong to  
a  
user but the conversation also has a receipient user with his/her  
corresponding conversation. How would I model that?

Thanks,  
Frank

---

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [January 21, 2006, 5:34am UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637/2 "2006-01-21T05:34:16Z")

</div>

well i think the message would have the recipient/sender not the  
conversation. the conversation is a one to many of conversation -\>  
messages.

i think you need to decide one way or the other if the messages have  
many users that are recipients or has\_many users that are senders. I  
dont think you can have both. Although you could have two fields in  
the messages table that hold user\_id , and do separate left joins on  
them. 1 to get the sender and one to get the recipient.

so in your messages table there would be a user\_id field which would  
be the sender of the message. You would also put a recipient\_id field  
, which would be a user\_id as well , however this would hold the  
recipient.

## sender

select \* from messages left join users on messages.user\_id = users.id

## recipient

select \* from messages left join users on messages.recipient\_id =  
users.id

make sense ?

adam

---

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [January 22, 2006, 2:15am UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637/3 "2006-01-22T02:15:09Z")

</div>

I don’t understand why you would have the recipient/sender in the  
message.  
It should be in the conversation because a conversation occurs between  
two  
people so why redundantly have that information in the messages?

I think it makes more sense that each party in the conversation has his  
own  
concept of the conversation with his own messages. That is, User A has  
Conversation A with User B’s Conversation B. However I’m stuck on how  
to  
represent that in a rails model. Would it be?

Conversation  
belongs\_to :user  
has\_many :messages  
has\_one: recipient\_conversation  
belongs\_to: sender\_conversation

---

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [January 24, 2006, 5:45am UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-threaded-messaging-system/63637/4 "2006-01-24T05:45:32Z")

</div>

Frank K. wrote:

> I don’t understand why you would have the recipient/sender in the  
> message. It should be in the conversation because a conversation occurs  
> between two people

I have email discussions with more than one person, and so do many other  
people. If you assume otherwise, you’ll just have to go back and  
rewrite your database and code to fix it when other people start using  
your program–because they’ll complain to no end that you only allowed  
for 2 people in a thread. (c:
