0

我作出了悄悄話系統和我使用狀態機知道哪裏是消息。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

非常感謝您

回答

0

它看起來像你想在這裏追蹤兩件事情(讀狀態和交貨狀態)以及你目前組合他們在一起。我認爲這確實是有道理的,並且將這些分離開來會更清潔。

我想提出以下建議:

  1. 在你的狀態機,跟蹤消息狀態:草案,發件箱,發送,收件箱,垃圾桶等

  2. 分別保持在模型read_at字段存儲該消息是由收件人查看併爲此值爲零的日期時間。

    field :read_at, type: DateTime, default: nil

  3. 添加一個布爾方法您的文檔檢查讀狀態:

    def read?
    [email protected]_at.nil?
    end

+0

非常感謝您! – hyperrjas

+0

我的榮幸。很高興它有幫助。 –