枚举不像Rails那样。状态机是。
时退房“过渡”宝石(link)(这几乎是Rails核心的一部分)
然后你就可以执行下列操作...
#GemFile
gem "transitions", :require => ["transitions", "active_record/transitions"]
#And in your Model, do something like the following:
include ActiveRecord::Transitions
field :state, type: String
scope :active, where(state: 'active')
state_machine do
state :active
state :inactive
event :inactivate do
transitions :from => :active, :to => :inactive
end
event :activate do
transitions :from => :inactive, :to => :active
end
end
这对我是一个过渡也不使用枚举和类型表 - 但我没有错过它们
这将与mongoid一起工作吗? – Blankman 2011-05-25 15:24:12
当然可以 - 我使用的代码来自Mongoid模型(mongoid是最好的!) – Jonathan 2011-05-25 15:32:15