state_machine in rails 4 初始化状态列值不正确
state_machine in rails 4 not proper initialize state column value
我正在学习 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
试试
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
我正在学习 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
试试
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