state_machine包含收件箱和已发送的邮件

时间:2012-10-01 20:28:01

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

我正在创建一个私人消息系统,我正在使用状态机来了解消息的位置。

这是我的模特:

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

的新手

非常感谢

1 个答案:

答案 0 :(得分:0)

看起来你正试图在这里追踪两件事(阅读状态和交付状态),你现在正在将它们组合在一起。我认为它实际上是有道理的,并且将这些分开来更加清晰。

我会推荐以下内容:

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

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

    field :read_at, type: DateTime, default: nil

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

    def read?
    !@read_at.nil?
    end