0

我正在尝试使用 aasmstate machine从一个state到另一个。但它issue是在没有调用的情况下statemachine通过所有的。states这是我正在使用的代码

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??

4

1 回答 1

0

问题原来是我有两个database fieldscheck_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
于 2017-02-04T13:36:24.537 回答