Hey, I'm trying to implement a message system as used in social networks. The problem I'm facing is first about the database structure and second how to implement it in rails.
My first idea is I'm using 3 tables:messages: id|subject|text|created_at
receivers: id|message_id|read:boolean creators: id|message_id|read:booleannow I'm wondering how to implement following features:
1.) a user can delete his message. but as both want to read the message, how to make sure the message is only deleted when both users have deleted it. 2.) how do I implement a reply? or actually how do I find the corresponding creator? 3.) how to find out whether a mail was read by the receiver?another idea is: creator_messages: id|creator_id|receiver_id|subject|text|read|created_at receiver_messages: same as creator_messages
this distinguishes between the users, so they can delete individually their messages. but how do i find out, whether the mail was read or not?
my third approach was basicly my second but only one table messages and then displaying this to the user.
1. the message is deleted as soon as one of the user deletes it. 2. actually how do I represent the relationships as has_many and belongs to?I thought it would work like this:
model User
:开发者_Python百科has_many :send_messages, :class_name=>"messages", :foreign_key=>"creator_id"
:has_many :received_messages, :class_name=>"messages", :foreign_key=>"receiver_id"
end
model Messages
belongs_to :user
end
but somehow I didn't get it to work. guess I'm missing something basic here.
Hope someone can help me =) thanks a lot
ok, if i understand it correctly the messages would have maximum of 1 receiver and 1 sender. In that case i would do the following:
I would create just a messages model, this would have the extra fields - receiver_read - receiver_deleted - sender_deleted
Now you can add hooks to the model like "after_save", "after_create" in here you can check if the receiver_read has just been set to true via for example with the receiver_read_changed? method, if this is true you can notify the sender or do something else with it. With this after_save hook you can also check that if the sender_deleted is just set to true and the receiver_deleted is already true you delete the whole message.
When you have multiple receivers I would create a join model for the receivers and have the sender_deleted and sender_id in the message model. In the join model i would add the columns receiver_id, read and deleted. Now i would use the before_save method on the messages and the join models to check if the message needs to be deleted or if the sender has to be notified of the message that has been read.
The solution might be:
1) I would create a deleted(_at) flag in the receivers table as well, if you really want to hard-remove the messages from the database if all of the receivers deleted it you could setup a cronjob or something.
2) have a creator_id in the messages model instead of a creators table, i mean how can multiple people create the same message?
3) I don't really get this one, i guess you set the "read" flag on the receivers table to "true" when that user opens up the message, after this you can make a scope in the users model like "scope :read, where(:read, true)" and also a scope unread.
I hope this is what you mean.
精彩评论