statemachine状态总是返回最后一个状态而不是initial

时间:2017-02-04 12:04:33

标签: ruby-on-rails ruby state-machine aasm

我正在尝试使用aasm state machinestate转到另一个issue。但statemachinestates在没有通话的情况下正在通过所有include AASM aasm column: 'state' do state :pending, initial: true state :checked_in state :checked_out event :check_in do transitions from: :pending, to: :checked_in, guard: :verify_payment? end event :check_out do transitions from: :checked_in, to: :checked_out end end def verify_payment? self.payment_status=="SUCCESS" end 。这是使用

的代码
Booking.create

此处如果我checked_out,它会返回pending状态,即使最初也不是预期的last

为什么它返回initial预期状态而不是{{1}} ??

1 个答案:

答案 0 :(得分:0)

问题原因是我有两个database fields名为check_incheck_out。因此activerecord会将其视为属性方法并在创建时触发这些事件。因此,此处的修复方法是将event名称更改为与数据库中的名称不同的名称

 include AASM

      aasm column: 'state' do
        state :pending, initial: true
        state :checked_in
        state :checked_out
        event :move_to_check_in do
          transitions from: :pending, to: :checked_in, guard: :verify_payment?
        end
        event :move_to_check_out do
          transitions from: :checked_in, to: :checked_out
        end
      end

      def verify_payment?
        self.payment_status=="SUCCESS"
      end