Forum: Ruby on Rails need some help designing my messaging system

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.
B7c24a7df391a1c318a90974537afc43?d=identicon&s=25 Ramin Bozorg (i8ramin)
on 2005-12-19 19:03
(Received via mailing list)
I am trying to create a messaging system for my users but I'm having a
hard time designing my db. This is what i have in mind, but I am not
sure if its the best approach.

user has_one inbox
user has_one outbox

inbox has_many messages
outbox has_many messages

inbox table
user_id

outbox table
user_id

messages table
box_id (refers to either inbox or outbox - how?)
from_id
to_id
msg_status (new, sent, read, deleted, etc.)
subject
body

does this make sense? is there a better way of acheiving this? since
both inbox and outbox both have many messages, how do I created the
association between them? will the addition of a from_id and to_id in
my messages table slow anything down? is there a better way or an OO
way of getting this information w/o hitting the db alot?

Thank you,
R
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-19 19:13
Ramin Bozorg wrote:
> I am trying to create a messaging system for my users but I'm having a
> hard time designing my db. This is what i have in mind, but I am not
> sure if its the best approach.
>
> user has_one inbox
> user has_one outbox
>
> inbox has_many messages
> outbox has_many messages

What do you need the boxes for? Why not:

message belongs_to sender, class = User
message belongs_to recipient, class = User

user has_many sent_messages, class = Message, foreign_key = sender_id
user has_many received_messages, class = Message, foreign_key =
recipient_id

table messages:
  sender_id
  recipient_id
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2005-12-19 20:12
(Received via mailing list)
you could take that a step further even to handle multiple recipients:

(untested)

users
----------
id int
...

messages
----------
id int
sender_id int
...

messages_recipients
----------
message_id int
recipient_id int
read_at  datetime default NULL
...

class User < ActiveRecord::Base
  # received messages (INBOX)
  has_and_belongs_to_many :received_messages, :class_name => "Message",
:join_table => "messages_recipients"

  # sent messages (OUTBOX)
  has_many :sent_messages, :class_name => "Message", :foreign_key =>
"sender_id"
end

class Message < ActiveRecord::Base
  # recipients of the message
  has_and_belongs_to_many :recipients, :class_name => "User",
:join_table =>
"messages_recipients", :association_foreign_key => "recipient_id"

  # sender of the message
  belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
end

then to take it even FURTHER and implement a 'read_at' field in the join
table to show what's been read and when.  this could allow you to
hilight
unread messages or even a simple read receipt.
B7c24a7df391a1c318a90974537afc43?d=identicon&s=25 Ramin Bozorg (i8ramin)
on 2005-12-19 20:42
(Received via mailing list)
That's the part I was confused about myself. I didn't think I needed
the boxes, but wasn't sure how to write the Rails code to handle it. I
dont quiet yet have a good understanding of how the ActiveRecord stuff
works. Can you guys recommend a chapter in the book that will explain
all of this better?


On 12/19/05, Chris Hall <christopher.k.hall@gmail.com> wrote:
>  ----------
>
>  class Message < ActiveRecord::Base
> unread messages or even a simple read receipt.
> > >
> > recipient_id
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>


--
- Ramin
http://www.getintothis.com/blog
B7c24a7df391a1c318a90974537afc43?d=identicon&s=25 Ramin Bozorg (i8ramin)
on 2005-12-19 20:42
(Received via mailing list)
Chris, thanks for the multiple recipients tip. Something I will
definitely consider for my application.

On 12/19/05, Ramin <i8ramin@gmail.com> wrote:
> >  (untested)
> >  ...
> >    has_and_belongs_to_many :received_messages, :class_name => "Message",
> > => "messages_recipients", :association_foreign_key => "recipient_id"
> > On 12/19/05, Andreas S. <f@andreas-s.net> wrote:
> > >
> > >   sender_id
> >
> - Ramin
> http://www.getintothis.com/blog
>


--
- Ramin
http://www.getintothis.com/blog
This topic is locked and can not be replied to.