1

我正在使用state_machine来管理对ActiveRecord::Base班级的批准。

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

# 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一边做。

这是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 之前将初始/默认值设置为“待定”。

是否可以定义我的状态预初始化?如果我可以将State对象或 a传递StateCollectionMachine初始化程序,那就太好了,但看起来不可能(查看源代码https://github.com/pluginaweek/state_machine/blob/master/lib/state_machine /machine.rb )。

4

0 回答 0