我正在制作一个私人消息系统,并且我正在使用状态机来了解消息在哪里。
这是我的模型:
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º 一条消息如何同时出现在同一地点sent
?inbox
2º 什么初始状态对发送者和接收者用户有消息?
抱歉,我是state_machine gem的新手
非常感谢你