我正在学习Rails并尝试使用状态机,但我无法初始化状态变量的值..谁能帮助我
class Primer < ActiveRecord::Base
extend StateMachine::MacroMethods
state_machine :initial => :data do
event :ignite do
transition :parked => :idling
end
end
end
数据库模式
class CreatePrimers < ActiveRecord::Migration
def change
create_table :primers do |t|
t.string :name
t.string :state
t.string :desc
t.timestamps null: false
end
end
end
try with
rails c
a = Primer.new
=> #<Primer id: nil, name: nil, state: nil, desc: nil, created_at: nil, updated_at: nil>
2.2.0 :002 > a.state
=> nil
似乎是一个已知问题https://github.com/pluginaweek/state_machine/issues/334
新的gem建议是https://github.com/state-machines/state_machines-activerecord