Forum: Ruby on Rails RESTful messaging. Trying to put it together and it feels u

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
80db2faa32727bbe4b3c98efed632e51?d=identicon&s=25 Rick Tessner (Guest)
on 2007-06-12 06:50
(Received via mailing list)
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
rick.tessner@gmail.com
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-06-12 08:07
Rick Tessner 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
80db2faa32727bbe4b3c98efed632e51?d=identicon&s=25 Rick Tessner (Guest)
on 2007-06-12 17:26
(Received via mailing list)
Hi Kad,

On Tue, 2007-12-06 at 08:07 +0200, Kad Kerforn 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
rick.tessner@gmail.com
This topic is locked and can not be replied to.