【问题标题】:state_machine with inbox and sent messages带有收件箱和已发送消息的状态机
【发布时间】:2012-10-01 20:28:01
【问题描述】:

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

这是我的模型:

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 的新手

非常感谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid state-machine


    【解决方案1】:

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

    我会推荐以下内容:

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

    2. 在模型中单独保留一个 read_at 字段,用于存储收件人查看邮件的日期时间,并将此值默认为 nil。

      field :read_at, type: DateTime, default: nil

    3. 在文档中添加一个布尔方法来检查读取状态:

      def read?
      !@read_at.nil?
      end

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多