# Need some help designing my messaging system

**URL:** <https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653>\
**Category:** Rails\
**Created:** [December 19, 2005, 7:03pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653 "2005-12-19T19:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [December 19, 2005, 7:03pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653/1 "2005-12-19T19:03:15Z")

</div>

I am trying to create a messaging system for my users but I’m having a  
hard time designing my db. This is what i have in mind, but I am not  
sure if its the best approach.

user has\_one inbox  
user has\_one outbox

inbox has\_many messages  
outbox has\_many messages

inbox table  
user\_id

outbox table  
user\_id

messages table  
box\_id (refers to either inbox or outbox - how?)  
from\_id  
to\_id  
msg\_status (new, sent, read, deleted, etc.)  
subject  
body

does this make sense? is there a better way of acheiving this? since  
both inbox and outbox both have many messages, how do I created the  
association between them? will the addition of a from\_id and to\_id in  
my messages table slow anything down? is there a better way or an OO  
way of getting this information w/o hitting the db alot?

Thank you,  
R

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [December 19, 2005, 7:13pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653/2 "2005-12-19T19:13:19Z")

</div>

Ramin Bozorg wrote:

> I am trying to create a messaging system for my users but I’m having a  
> hard time designing my db. This is what i have in mind, but I am not  
> sure if its the best approach.
> 
> user has\_one inbox  
> user has\_one outbox
> 
> inbox has\_many messages  
> outbox has\_many messages

What do you need the boxes for? Why not:

message belongs\_to sender, class = User  
message belongs\_to recipient, class = User

user has\_many sent\_messages, class = Message, foreign\_key = sender\_id  
user has\_many received\_messages, class = Message, foreign\_key =  
recipient\_id

table messages:  
sender\_id  
recipient\_id

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [December 19, 2005, 8:12pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653/3 "2005-12-19T20:12:39Z")

</div>

you could take that a step further even to handle multiple recipients:

(untested)

## users

id int  
…

## messages

id int  
sender\_id int  
…

## messages\_recipients

message\_id int  
recipient\_id int  
read\_at datetime default NULL  
…

class User \< ActiveRecord::Base

# received messages (INBOX)

has\_and\_belongs\_to\_many :received\_messages, :class\_name =\> “Message”,  
:join\_table =\> “messages\_recipients”

# sent messages (OUTBOX)

has\_many :sent\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“sender\_id”  
end

class Message \< ActiveRecord::Base

# recipients of the message

has\_and\_belongs\_to\_many :recipients, :class\_name =\> “User”,  
:join\_table =\>  
“messages\_recipients”, :association\_foreign\_key =\> “recipient\_id”

# sender of the message

belongs\_to :sender, :class\_name =\> “User”, :foreign\_key =\> “sender\_id”  
end

then to take it even FURTHER and implement a ‘read\_at’ field in the join  
table to show what’s been read and when. this could allow you to  
hilight  
unread messages or even a simple read receipt.

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [December 19, 2005, 8:42pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653/4 "2005-12-19T20:42:53Z")

</div>

That’s the part I was confused about myself. I didn’t think I needed  
the boxes, but wasn’t sure how to write the Rails code to handle it. I  
dont quiet yet have a good understanding of how the ActiveRecord stuff  
works. Can you guys recommend a chapter in the book that will explain  
all of this better?

On 12/19/05, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> * * *
> 
> class Message \< ActiveRecord::Base  
> unread messages or even a simple read receipt.
> 
> > > 
> > 
> > recipient\_id  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Ramin  
[http://www.getintothis.com/blog](http://www.getintothis.com/blog)

---

<div class="post-metadata">

**Author:** ![i8ramin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e36b37/32.png) [@i8ramin](https://www.ruby-forum.com/u/i8ramin)\
**Post date:** [December 19, 2005, 8:42pm UTC](https://www.ruby-forum.com/t/need-some-help-designing-my-messaging-system/50653/5 "2005-12-19T20:42:55Z")

</div>

Chris, thanks for the multiple recipients tip. Something I will  
definitely consider for my application.

On 12/19/05, Ramin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > (untested)  
> > …  
> > has\_and\_belongs\_to\_many :received\_messages, :class\_name =\> “Message”,  
> > =\> “messages\_recipients”, :association\_foreign\_key =\> “recipient\_id”  
> > On 12/19/05, Andreas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > sender\_id
> 
> - Ramin  
> [http://www.getintothis.com/blog](http://www.getintothis.com/blog)

–

- Ramin  
[http://www.getintothis.com/blog](http://www.getintothis.com/blog)
