# Messaging system using Rails

**URL:** <https://www.ruby-forum.com/t/messaging-system-using-rails/65662>\
**Category:** Rails\
**Created:** [August 20, 2006, 8:59am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662 "2006-08-20T08:59:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 20, 2006, 8:59am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/1 "2006-08-20T08:59:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 20, 2006, 9:04am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/2 "2006-08-20T09:04:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 20, 2006, 9:54am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/3 "2006-08-20T09:54:26Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > If I have a join table like messages\_users, it would have a message ID  
> > Hi,  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen  
[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 20, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/4 "2006-08-20T12:33:21Z")

</div>

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](http://www.petercooper.co.uk/archives/001038.html) ?

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 20, 2006, 10:09am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/5 "2006-08-20T10:09:13Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > If I have a join table like messages\_users, it would have a message ID  
> > > Hi,  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> [www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen  
> [www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 21, 2006, 8:52am UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/6 "2006-08-21T08:52:29Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> other way.
> 
> > 
> 
> >

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen  
[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://www.ruby-forum.com/u/Binh_Ly)\
**Post date:** [August 21, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/messaging-system-using-rails/65662/7 "2006-08-21T16:29:32Z")

</div>

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.
