I plan to implement a private message system between members. I'm wondering what are the preferred approaches to this.
Requirements are
I should be able to retrieve them easily as something like this
@user.conversations #Should return User objects that I sent or received messages from (but not me) @user.conversations.messages #Messages from all or specific user objects. @user.conversations.messages.unread #Unread messages
When calling @user.conversations should retrieve only the people that sent me messages or people I send messages to. current_user should be excluded.
If i'm sender_id=5 and send to_id=10 then, the other person would reply as sender=10 to_id=5. This should be considered and understood as the same conversation object.
Regarding last point. I'm not sure what's the preferred approach to modeling.
It's preferred to use one Conversation model to handle all messages such as
attr_accessible :user_id, :friend_id, :message, :read
belongs_to :user
Or it's preferred to create a Conversation model to handle association and a Message model for messages.
I would like to see sample cases of how to implement this relationship and if there's additional method to implement.
I'm a bit lost here.