# Sending messages between users

**URL:** <https://www.ruby-forum.com/t/sending-messages-between-users/160826>\
**Category:** Rails\
**Created:** [February 20, 2009, 2:36am UTC](https://www.ruby-forum.com/t/sending-messages-between-users/160826 "2009-02-20T02:36:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [February 20, 2009, 2:36am UTC](https://www.ruby-forum.com/t/sending-messages-between-users/160826/1 "2009-02-20T02:36:57Z")

</div>

Hi,

I’m trying to create an web app where users can write messages to each  
other. I use the restful\_authentication plugin for the users. So they  
have  
an user\_id. How ever the messages should have a:

- sender
- receiver
- content

As i saw in some screencast the association use the user\_id to connect  
users  
to something. But in my model there would be 2 user\_id column which  
obviously doesn’t work. So how do I do it?

My workaround was to create a receiver and sender model which contained  
a  
user\_id and a message\_id. Then the message contained a sender\_id,  
receiver\_id and the content. However now I’m stuck somehow. _Maybe it’s  
because 2:30am here and I should be in bed_. I don’t know. Is there an  
easier workaround? Some tutorials, maybe?

How do I collect my mails? Have a controller named messages where I  
tried  
this, but it doesn’t work (I thing my association declaration are the  
cause):  
@inbox = current\_user.message.find\_by\_receiver\_id(:user)

_The Models:_

class _Message_ \< ActiveRecord::Base  
belongs\_to :sender  
has\_one :receiver

validates\_presence\_of :sender  
validates\_presence\_of :receiver  
validates\_length\_of :content, :maximum =\> 4096  
end

class _Receiver_ \< ActiveRecord::Base  
belongs\_to :message  
end

class _Sender_ \< ActiveRecord::Base  
has\_many :message  
end

class _User_ \< ActiveRecord::Base (restful\_authentication plugin)

# Virtual attribute for the unencrypted password

attr\_accessor :password  
…  
attr\_accessible :login, :email, :password, :password\_confirmation  
…  
end

Greetings and thanks for any kind of help

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [February 20, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/sending-messages-between-users/160826/2 "2009-02-20T15:54:09Z")

</div>

Chris,

Why don’t you use (or check how it’s done) restful easy messages?  
Alternatively, LOVD ([lovdbyless.com](http://lovdbyless.com)) does it all.

Cheers, Sazima

On Feb 19, 10:35 pm, Christoph J.

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [February 20, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/sending-messages-between-users/160826/3 "2009-02-20T16:20:37Z")

</div>

Hello Chris, may I try to help you?  
I thing you need something like this:

class _Message_ \< ActiveRecord::Base

belongs\_to :sender, :class\_name =\> ‘User’, :foreign\_key =\>  
‘sender\_id’  
belongs\_to :receiver, :class\_name =\> ‘User’, :foreign\_key =\>  
‘receiver\_id’

end

class _User_ \< ActiveRecord::Base (restful\_authentication plugin)

# Virtual attribute for the unencrypted password

attr\_accessor :password  
…  
attr\_accessible :login, :email, :password, :password\_confirmation

has\_many :sent\_messages, :class\_name =\> ‘Message’ , :foreign\_key =\>  
‘sender\_id’  
has\_many :received\_messages, :class\_name =\> ‘Message’ , :foreign\_key  
=\> ‘receiver\_id’  
…  
end

and then you can do this:  
@inbox = current\_user.received\_messages.find\_by\_receiver\_id(:user)

I’m dont test this, but it shold work)  
Sorry for my english.
