我正在学习 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
尝试
导轨 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