messagebox - Rails private message system -
hey, i'm trying implement message system used in social networks. problem i'm facing first database structure , second how implement in rails.
first idea i'm using 3 tables:
messages: id|subject|text|created_at
receivers: id|message_id|read:boolean
creators: id|message_id|read:boolean
now i'm wondering how implement following features:
1.) user can delete message. both want read message, how make sure message deleted when both users have deleted it.
2.) how implement reply? or how find corresponding creator?
3.) how find out whether mail read receiver?
another idea is: creator_messages: id|creator_id|receiver_id|subject|text|read|created_at receiver_messages: same creator_messages
this distinguishes between users, can delete individually messages. how find out, whether mail read or not?
my third approach basicly second 1 table messages , displaying user.
1. message deleted 1 of user deletes it.
2. how represent relationships has_many , belongs to?
i thought work this:
model user :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 didn't work. guess i'm missing basic here.
hope can me =) lot
ok, if understand correctly messages have maximum of 1 receiver , 1 sender. in case following:
i create messages model, have fields - receiver_read - receiver_deleted - sender_deleted
now can add hooks model "after_save", "after_create" in here can check if receiver_read has been set true via example receiver_read_changed? method, if true can notify sender or else it. after_save hook can check if sender_deleted set true , receiver_deleted true delete whole message.
when have multiple receivers create join model receivers , have sender_deleted , sender_id in message model. in join model add columns receiver_id, read , deleted. use before_save method on messages , join models check if message needs deleted or if sender has notified of message has been read.
Comments
Post a Comment