【发布时间】: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º 一条消息怎么会同时出现在sent 或inbox 的地方?
2º 消息发送者和接收者用户的初始状态是什么?
抱歉,我是 state_machine gem 的新手
非常感谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid state-machine