# Internal mail system : model definition issue

**URL:** <https://www.ruby-forum.com/t/internal-mail-system-model-definition-issue/164344>\
**Category:** Rails\
**Created:** [March 31, 2009, 11:09am UTC](https://www.ruby-forum.com/t/internal-mail-system-model-definition-issue/164344 "2009-03-31T11:09:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![akyrho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@akyrho](https://www.ruby-forum.com/u/akyrho)\
**Post date:** [March 31, 2009, 11:09am UTC](https://www.ruby-forum.com/t/internal-mail-system-model-definition-issue/164344/1 "2009-03-31T11:09:06Z")

</div>

Hi all,

I try to implement an internal mail system to my rails app. I found this  
message [1] on another mailing list, but i need it to be a little  
different.

So here’s the case : i got a user table, a message table, and a  
communication table. This last one contains the message\_id, the  
sender\_id and the receiver\_id. I need it for some future improvment.

# user.rb

has\_many :communications, :dependent =\> :destroy  
has\_many :sent\_messages, :class\_name =\> “Message”, :through =\>  
:communications, :source =\> :sender, :order =\> ‘created\_at DESC’  
has\_many :received\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“receiver\_id”, :order =\> ‘created\_at DESC’  
has\_many :unread\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“receiver\_id”, :conditions =\> [‘read\_at IS NULL’], :order =\> ‘created\_at  
DESC’

# communication.rb

belongs\_to :sender, :class\_name =\> “User”, :foreign\_key =\> “sender\_id”  
belongs\_to :receiver, :class\_name =\> “User”, :foreign\_key  
=\>“receiver\_id”  
has\_one :message

# message.rb

belongs\_to :communication

But when i try to get some\_user.sent\_message, it returns me this error :

ActiveRecord::StatementInvalid: PGError: ERROR: column  
communications.user\_id does not exist  
LINE 1: …messages.id = communications.sender\_id WHERE  
(("communica…  
^  
: SELECT “messages”.\* FROM “messages” INNER JOIN communications ON  
messages.id = communications.sender\_id WHERE  
((“communications”.user\_id = 1)) ORDER BY created\_at DESC

Obviously, something went wrong and Rails try to get the  
communications.user\_id id place of communications.sender\_id.

Any help would be appreciate. Thanks 😉

* * *

[1]  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/7fdccddde9b97ffc/6112294580b9e0ce?lnk=gst&q=internal+mail&rnum=1](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7fdccddde9b97ffc/6112294580b9e0ce?lnk=gst&q=internal+mail&rnum=1)
