# RESTful messaging. Trying to put it together and it feels u

**URL:** <https://www.ruby-forum.com/t/restful-messaging-trying-to-put-it-together-and-it-feels-u/106583>\
**Category:** Rails\
**Created:** [June 12, 2007, 6:50am UTC](https://www.ruby-forum.com/t/restful-messaging-trying-to-put-it-together-and-it-feels-u/106583 "2007-06-12T06:50:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [June 12, 2007, 6:50am UTC](https://www.ruby-forum.com/t/restful-messaging-trying-to-put-it-together-and-it-feels-u/106583/1 "2007-06-12T06:50:34Z")

</div>

Hi all,

I’m putting together a quick little app just to do some messaging  
between users. I’m trying to do it restfully but what I have feels kind  
of unclean.

Here’s what I have:

Models:  
User has\_many :messages  
Message belongs\_to :user ; belongs\_to :msgcontent  
Msgcontent has\_many :messages

Message is a join model between User and Msgcontent. In addition to  
containing references to User and Msgcontent, it also has a “status”  
field indicating whether a given user has read or deleted the message.  
This is the only reason for the join model. It saves having to have the  
same content for a message replicated multiple times in the database.

Controllers:  
UsersController  
SessionsController  
MessagesController

The UsersController and SessionsController come from the  
restful\_authentication plugin. Note that there is _no_  
MsgcontentsController.

The intent is to have the following urls, but it feels dirty since it’s  
mixing the Message and Msgcontent models together in the  
MessagesController. It does leave the url space fairly clean.

```
  * GET /messages # retrieves messages & msgcontent for current
    user
  * POST /messages # creates single msgcontent and a message for
    each recipient
  * GET /messages/1 # retrieves message 1 and related msgcontent
  * PUT /messages/1 # updates the status of message 1 (read/unread
    status)
  * DELETE /messages/1 # deletes message 1 and if no more references
    to related msgcontent, removes that as well.
  * GET /messages/new # form containing info for _msgcontent_
  * GET /messages/1/edit # possible form for _message_ to update
    status

```

I’m looking for alternate ways to solve this type of solution. Or is  
this how it would be done?

–  
Rick  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [June 12, 2007, 8:07am UTC](https://www.ruby-forum.com/t/restful-messaging-trying-to-put-it-together-and-it-feels-u/106583/2 "2007-06-12T08:07:22Z")

</div>

Rick T. wrote:

> Hi all,
> 
> I’m putting together a quick little app just to do some messaging  
> between users. I’m trying to do it restfully but what I have feels kind  
> of unclean.
> 
> Here’s what I have:
> 
> Models:  
> User has\_many :messages  
> Message belongs\_to :user ; belongs\_to :msgcontent  
> Msgcontent has\_many :messages

Hi Rick I am currentlt trying to solve the same problem in my app…  
I don’t understand your model… I believe the msg content is part of  
your message (as the subject)  
you should also have senders and receivers ?..

here is my model… what do u think ?

user.rb  
has\_many :messages\_as\_sender, :foreign\_key =\> ‘from\_id’, :class\_name  
=\> ‘Message’  
has\_many :messages\_as\_receiver, :foreign\_key =\> ‘to\_id’, :class\_name  
=\> ‘Message’  
has\_many :senders, :through =\> :messages\_as\_sender  
has\_many :receivers, :through =\> :messages\_as\_receiver

message.rb  
belongs\_to :sender, :foreign\_key =\> ‘from\_id’, :class\_name =\> “User”  
belongs\_to :receiver, :foreign\_key =\> ‘to\_id’, :class\_name =\> “User”

and in my routes.rb , I wrote

map.resources :users do |users|  
users.resources :messages  
end

…

so to send a message from user 5, I write users/5/messages/new  
to list all messages sent by user 5, I write users/5/messages  
… and so on

presently my pending problem is how to write a route to send a message  
to a specific user (from 5 to 25 for example)…

something like users/5/messages/25;send  
in this case I get the correct parameters, but also an error… I  
should add a member in the routes ?

Parameters: {“action”=\>“send”, “id”=\>“25”, “controller”=\>“messages”,  
“user\_id”=\>“5”}  
wrong number of arguments (1 for 0)

kad

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [June 12, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/restful-messaging-trying-to-put-it-together-and-it-feels-u/106583/3 "2007-06-12T17:26:22Z")

</div>

Hi Kad,

On Tue, 2007-12-06 at 08:07 +0200, Kad K. wrote:

> your message (as the subject)  
> you should also have senders and receivers ?..

The goal was to have only a single copy of a message and have multiple  
recipients have pointers to that single message. In this case the  
single message would be Msgcontent and the reference to the message  
would be Message.

I’m thinking this may be a premature optomization on my part. As long  
as the messages are small, each recipient can have a copy of it. It  
simplifies the models and controllers (basically the structure you  
outlined in your message).

Thanks for the reality check. 🙂

> presently my pending problem is how to write a route to send a  
> message  
> to a specific user (from 5 to 25 for example)…
> 
> something like users/5/messages/25;send  
> in this case I get the correct parameters, but also an error… I  
> should add a member in the routes ?

You should just be able to do a POST to /users/25/messages (assuming  
you’re doing some sort of authentication and have a current\_user object)  
and this would create a new message for User 25 (with the sender being  
the current\_user)

–  
Rick  
removed\_email\_address@domain.invalid
