# Messaging - the best way to implement?

**URL:** <https://www.ruby-forum.com/t/messaging-the-best-way-to-implement/125657>\
**Category:** Rails\
**Created:** [January 11, 2008, 12:21am UTC](https://www.ruby-forum.com/t/messaging-the-best-way-to-implement/125657 "2008-01-11T00:21:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [January 11, 2008, 12:21am UTC](https://www.ruby-forum.com/t/messaging-the-best-way-to-implement/125657/1 "2008-01-11T00:21:38Z")

</div>

I’m developing a messaging service for an application. It’s not an  
instant messaging, it’s more similar to how e-mail works. I send a  
message to user, user receives the message, I have a copy of the  
message sent by me in ‘sent’ folder.

That’s what the question is about. User gets the message from me and I  
have a copy of that message in ‘sent’ folder. Where to store both of  
them? Do I need different tables within database - one for messages  
and another one for sent messages? Or it could be working well with  
one table? Do I really need to make a copy of message in order to make  
a sent message? Or it could be done without data redundancy, i.e. with  
one entry in one table per message?

That’s not the question particularly about rails, but I’m not aware  
where to ask. Someone should have done something similar. Any advices?

Thanks.

---

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [January 11, 2008, 12:25am UTC](https://www.ruby-forum.com/t/messaging-the-best-way-to-implement/125657/2 "2008-01-11T00:25:08Z")

</div>

I’d define it like this:

User model  
has\_many :from\_messages. :class\_name =\> “Message”, :foreign\_key =\>  
“from\_id”  
has\_many :to\_messages, :class\_name =\> “Message”, :foreign\_key =\> “to\_id”

Message model:  
belongs\_to :to, :class\_name =\> “User”, :foreign\_key =\> “user\_id”  
belongs\_to :from, :class\_name =\> “User”, :foreign\_key =\> “user\_id”

I think in Rails 2.0 you don’t need to specify the foreign keys for the  
belongs\_to on messages because it’ll guess it from the class name.

On Jan 11, 2008 9:50 AM, truetype [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> one table? Do I really need to make a copy of message in order to make  
> a sent message? Or it could be done without data redundancy, i.e. with  
> one entry in one table per message?
> 
> That’s not the question particularly about rails, but I’m not aware  
> where to ask. Someone should have done something similar. Any advices?
> 
> Thanks.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![truetype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@truetype](https://www.ruby-forum.com/u/truetype)\
**Post date:** [January 11, 2008, 6:28am UTC](https://www.ruby-forum.com/t/messaging-the-best-way-to-implement/125657/3 "2008-01-11T06:28:55Z")

</div>

Also, to save you a lot of time. There are some GREAT messaging  
plugin’s that handle all the basic work and features up on the  
agilewebdevelopment site.

[http://agilewebdevelopment.com/plugins/search?search=messaging](http://agilewebdevelopment.com/plugins/search?search=messaging)

acts\_as\_messagable sounds like it’ll do what you want and more.  
Restful\_easy\_messages sounds good too.
