0

I am trying to use aasm state machine for going from one state to another. But the issue is that the statemachine is moving through all states without calling. Here is the code am using

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

Here If I do Booking.create it returns with checked_out state even initially instead of the expected pending

Why its returning the last expected state instead of initial ??

Abhilash
  • 2,864
  • 3
  • 33
  • 67

1 Answers1

0

The issue turned out to be that I have two database fields called check_in and check_out. So activerecord will consider it as attribute methods and fire those events on creation.So the fix here is to change the event name to something different than that in the database

 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
Abhilash
  • 2,864
  • 3
  • 33
  • 67