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’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:
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.
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.