状态机状态总是返回最后一个状态而不是初始状态
statemachine state is always returning the last state instead of initial
我正在尝试使用 aasm state 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
它 returns 甚至在初始状态 checked_out
而不是预期的 pending
为什么它返回 last
预期状态而不是 initial
??
问题原来是我有两个 database fields
,分别是 check_in
和 check_out
。所以 activerecord
会将其视为属性方法并在 creation.So 上触发这些事件这里的解决方法是将 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
我正在尝试使用 aasm state 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
它 returns 甚至在初始状态 checked_out
而不是预期的 pending
为什么它返回 last
预期状态而不是 initial
??
问题原来是我有两个 database fields
,分别是 check_in
和 check_out
。所以 activerecord
会将其视为属性方法并在 creation.So 上触发这些事件这里的解决方法是将 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