枚舉不像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