Forum: Ruby on Rails need some help designing my threaded messaging system

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Frank K. (Guest)
on 2006-01-21 06:19
(Received via mailing list)
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
Adam D. (Guest)
on 2006-01-21 06:34
(Received via mailing list)
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
Frank K. (Guest)
on 2006-01-22 03:15
(Received via mailing list)
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
iStormy (Guest)
on 2006-01-24 06:45
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:
This topic is locked and can not be replied to.