Messaging system using Rails

Hello,

I’m trying to figure out a way to model a messaging system with Rails.
For example, I want to do something like on Facebook where one user can
send another user a message. I’m having trouble figuring out how this
is said using ActiveRecord.

If I have a join table like messages_users, it would have a message ID
and a user ID but how do I specifiy who is the messege sender and how is
the receiver? Also, how would this work if I had one user spending a
message to like 5 other users on the system?

If anyone could provide me with some hints I’d greatly appreciate it.

Thanks in advance,

Binh Ly

Binh Ly wrote:

Hello,

I’m trying to figure out a way to model a messaging system with Rails.
For example, I want to do something like on Facebook where one user can
send another user a message. I’m having trouble figuring out how this
is said using ActiveRecord.

If I have a join table like messages_users, it would have a message ID
and a user ID but how do I specifiy who is the messege sender and how is
the receiver? Also, how would this work if I had one user spending a
message to like 5 other users on the system?

If anyone could provide me with some hints I’d greatly appreciate it.

Thanks in advance,

Binh Ly

Hi,

IMHO, you do not need any join table. Message model belongs_to sender
and receiver. Just create a model Message, and add two foreign keys
(sender, receiver). On User model, you have two has_many (sent_messages,
received_messages for example)

Jean-Etienne

A Message should have 1 sender and 1…n receivers

class Message < ActiveRecord:Base
has_one sender, :class_name => :user #or whatever
has_and_belongs_to_many :receivers, :class_name => :user
end

like this it can be modelled (dunno if thats completely correct code)

2006/8/20, Jean-Etienne D. [email protected]:

If I have a join table like messages_users, it would have a message ID
Hi,
Posted via http://www.ruby-forum.com/.


Michael S. [email protected]

www.siebert-wd.de - Gedanken lesen
www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

Binh Ly wrote:

Hello,

I’m trying to figure out a way to model a messaging system with Rails.
For example, I want to do something like on Facebook where one user can
send another user a message. I’m having trouble figuring out how this
is said using ActiveRecord.

If I have a join table like messages_users, it would have a message ID
and a user ID but how do I specifiy who is the messege sender and how is
the receiver? Also, how would this work if I had one user spending a
message to like 5 other users on the system?

If anyone could provide me with some hints I’d greatly appreciate it.

Thanks in advance,

Binh Ly

Have you seen the source code of this chat application done with Rails:
http://www.petercooper.co.uk/archives/001038.html ?

Michael,

your suggestion of:

has_and_belongs_to_many :receivers, :class_name => :user

means that I would have to create a join table as well? the
messages_users mentioned in the original post? i can’t see it done any
other way.

binh

Michael S. wrote:

A Message should have 1 sender and 1…n receivers

class Message < ActiveRecord:Base
has_one sender, :class_name => :user #or whatever
has_and_belongs_to_many :receivers, :class_name => :user
end

like this it can be modelled (dunno if thats completely correct code)

2006/8/20, Jean-Etienne D. [email protected]:

If I have a join table like messages_users, it would have a message ID
Hi,
Posted via http://www.ruby-forum.com/.


Michael S. [email protected]

www.siebert-wd.de - Gedanken lesen
www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

thats exactly right, altough you could use a join model, too (via
has_many
:through) to be able to mark the message as read or deleted via the join
(among other stuff).

2006/8/20, Binh Ly [email protected]:

other way.


Michael S. [email protected]

www.siebert-wd.de - Gedanken lesen
www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

Another option might be to use a decorated join model. soething like,

has_many :messages, :through => :communications

communications could have fields for message_id, recipient_id,
sender_id, date_sent, etc
also you could have a field type to store single table inheritance
information to know if a message is a post, comment or reply.

The possibilities are endless.
Good luck.