Forum: Ruby on Rails Need some advice on DB design

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.
Ramin B. (Guest)
on 2005-12-06 22:26
(Received via mailing list)
Hey everyone. I'm trying to create a table of "messages" for my users.
It works just like an email application. Each user can send and
receive messages to/from other users in the system.

I'm not sure what sort of relationship to create between the user and
the messages. Is it has_many or has_and_belongs_to_many? I want each
message to have the following information:

from
to
subject
body
status (new, read, replied, trashed)
type (user_msg, system_msg, friend_request, etc.)

I also want to be able to filter the messages based on their status
for the user. So they can view all their sent messages, new messages,
etc.

I appreciate any feedback. thanks.
krishna.vivek (Guest)
on 2005-12-07 14:30
(Received via mailing list)
> I'm not sure what sort of relationship to create between the user and
> the messages. Is it has_many or has_and_belongs_to_many? I want each
> message to have the following information:


 has_many
atomgiant (Guest)
on 2005-12-07 19:12
(Received via mailing list)
I believe it should be

 user has_many messages
 message has_one user

Also, you may want to rethink your statuses.  A message could be both
read and replied, or read and trashed.  Perhaps have a flag for
read/unread separate from the status.

You may also want to add a created_at field for your messages to keep
track of their when they occurred.  And perhaps even add a parent_id
for the parent message if you plan on supporting conversation chains.

Tom
Ramin B. (Guest)
on 2005-12-07 19:28
(Received via mailing list)
Thanks alot Tom. I'll work on your suggestions.

Is parent_id part of the whole ActiveRecord magic? or is it something
that I have to manually set and then save when working with my model?

Has anyone written a full messaging system in their application and
would care to share their code? I would only use it to learn and make
my own and not just copy it.

Thank you


On 12/7/05, Tom D. <removed_email_address@domain.invalid> wrote:
> track of their when they occurred.  And perhaps even add a parent_id
> > the messages. Is it has_many or has_and_belongs_to_many? I want each
> > for the user. So they can view all their sent messages, new messages,
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Ramin
http://www.getintothis.com/blog
Pat M. (Guest)
on 2005-12-07 20:13
(Received via mailing list)
On 12/7/05, Tom D. <removed_email_address@domain.invalid> wrote:
> I believe it should be
>
>  user has_many messages
>  message has_one user

That should be message belongs_to user
atomgiant (Guest)
on 2005-12-07 21:56
(Received via mailing list)
Good catch Pat... my mistake.

The parent_id would have to be implemented by you.  The created_at is
built in to RoR.

Tom
Pat M. (Guest)
on 2005-12-07 23:13
(Received via mailing list)
One other thing, saying that he has to implement parent_id is a bit
misleading.  What he needs to do is put an integer field called
parent_id in the messages table, and then to assign a parent to a
message:
messageobject.parent = parentobject

Or he can access it through the Parent#messages array:
parentobject.messages << messageobject

Tom isn't wrong, I just wanted to make sure that nobody misunderstands
and starts doing stuff like
messageobject.parent_id = parentobject.id

Pat
This topic is locked and can not be replied to.