在state_machine初始化之前设置状态值

时间:2013-08-09 16:39:48

标签: ruby-on-rails state-machine

我正在使用state_machine来管理ActiveRecord::Base课程的审批。

我正在使用自定义state值,因此我可以将状态存储为整数。这很有效,除了我在Rails启动时收到以下警告(来自StateMachine::Machine):

# Both MyClass and its :state machine have defined a different default
# for "state". Use only one or the other for defining defaults to avoid
# unexpected behaviors.

我没有观察到任何意外行为,所以这不是什么大问题。我知道我可以从表格架构中移除:default值(例如:default => 0)以使此错误消失,但我更愿意在state_machine侧执行此操作。< / p>

以下是state_machine代码(在my_class.rb中):

# ...
States = {
  :pending => 0,
  :approved => 1,
  :rejected => 2,
}

state_machine :state, :initial => :pending do
  States.each do |name, value|
    state name, :value => value
  end

  event :approve do
    transition all => :approved
  end

  event :reject do
    transition all => :rejected
  end
end

问题是state_machine想要在确认默认值实际为0之前将初始值/默认值设置为“pending”。

是否可以在初始化前定义我的状态?如果我可以将State个对象或StateCollection传递给Machine初始化程序,那会很好,但看起来不可能(查看{{3}处的来源) })。

0 个答案:

没有答案
相关问题