0

我正在尝试在用户之间实现消息传递系统。

这是我要实现的数据库模型。

user
  id

message
  message_id
  message_author_id
  timestamp

message_recipient
  message_id
  user_id

message_status
  message_status_id
  message_id
  user_id
  is_read
  is_deleted
  read_datetime
  deleted_datetime

我相信这种关系会跟随

user
  has_many :messages
  has_many :message_recipient
  has_many :message_status

message
  has_many: user => author_id

message_recipient
  belongs_to :message
  belongs_to user

message_status
  belongs_to :message
  belongs_to :user

首先,我不确定数据库是否是正确的实现方式,有很多接收者,也不确定它是否是正确的 rails 方式。

其次,目标是实现 jquery.tokeninputs,因此据我了解,我需要确保有很多用户能够使用许多用户的作者字段。

4

1 回答 1

0

关于您的实施,我认为您可能有一个冗余模型 - 取决于您要做什么,但它会像这样:

class User < ActiveRecord::Base
  has_many :messages
  has_many :sent_messages, through: :messages 
  has_many :destined_messages, class_name: 'SentMessage'
end

# this class represents the original authored message,
# i.e. it holds the body of the message
class Message < ActiveRecord::Base
  belongs_to :user # author
  has_many :sent_messages
end

# this class represents a "copy" of the message 
# that has been sent to a particular user. 
# It also holds the status, which makes sense because
# you want to log the status according to each user
class SentMessage < ActiveRecord::Base
  belongs_to :message
  belongs_to :user
end

如果您不在乎重复,您甚至可以合并模型:只需为每个收件人制作正文的 Message副本。SentMessage

于 2013-03-13T09:29:40.793 回答