我想建议如下进行关联
class User < ActiveRecord::Base
has_many :messages_received, :class_name => 'Message', :foreign_key=> 'to_user_id'
has_many :messages_sent, :class_name => 'Message', :foreign_key=> 'from_user_id'
end
class Message < ActiveRecord::Base
belongs_to :from_user, :class_name => 'User' # from_user_id field fk Users
belongs_to :to_user, :class_name => 'User' # to_user_id field fk Users
belongs_to :thread, :class_name => 'Message' # Reference to parent message
has_many :replies, :class_name => 'Message', :foreign_key => 'thread_id'
end
您可以按如下方式创建消息s
first_msg = Message.new(:to_user => userA, :from_user => userB, :body => 'Hello!')
userA_reply = first_msg.replies.build(:to_user => userA, :from_user => userB, :body => 'hi back')
userB_reply = first_msg.replies.build(:to_user => userB, :from_user => userA, :body => 'later')
并且更容易检查用户发送的消息为@user.messages_sent
并作为@user.messages_received 接收
a) 您可以使用https://jqueryui.com/autocomplete/自动完成电子邮件
b) 您可以使用布尔字段已读、未读和已丢弃