0

我正在制作一个私人消息系统,并且我正在使用状态机来了解消息在哪里。

这是我的模型:

class Message
  include Mongoid::Document
  include Mongoid::Timestamps::Created

  #fields
  field :subject, :type => String
  field :body, :type => String
  field :place, :type => String
  field :has_been_read, :type => String

  # Relationships 
  belongs_to :sender, :class_name => 'User', :inverse_of => :messages_sent
  belongs_to :receiver,   :class_name => 'User', :inverse_of => :messages_received

  #state machine has been read message?
    state_machine :has_been_read, :initial => :unread do
    event :read_message do
    transition :from => :unread, :to => :read
   end
   event :mark_unread do
     transition :from => :read, :to => :unread
   end
  end
  #state machine status can be in_box, sent, draft, trash, spam
end

用户模型:

class User
 include Mongoid::Document
 include Mongoid::Timestamps::Created
.
.
.
  has_many :messages_sent, :class_name => 'Message', :inverse_of => :sender
  has_many :messages_received, :class_name => 'Message', :inverse_of => :receiver
.
.
.
end

1º 一条消息如何同时出现在同一地点sentinbox

2º 什么初始状态对发送者和接收者用户有消息?

抱歉,我是state_machine gem的新手

非常感谢你

4

1 回答 1

0

看起来您正在尝试在此处跟踪两件事(读取状态和交付状态),并且您目前正在将它们组合在一起。我认为这实际上是有道理的,并且将它们分开会更干净。

我会推荐以下内容:

  1. 在您的状态机中,跟踪消息状态:草稿、发件箱、已发送、收件箱、垃圾箱等。

  2. Separately keep a read_at field in your model that stores the datetime that the message was viewed by the recipient and default this value to nil.

    field :read_at, type: DateTime, default: nil

  3. Add a boolean method to your document for checking read status:

    def read?
    !@read_at.nil?
    end

于 2012-10-03T17:06:21.473 回答