我有一个预订模型,可以使用AASM更改状态
models / booking.rb
before_validation :item_availability, if: -> { pending? || approved? }
enum status: [:pending, :approved, :rejected, :on_loan, :returned]
aasm column: :status, enum: true, whiny_transitions: true do
state :pending, initial: true
state :approved
event :approve do
transitions from: :pending, to: :approved
end
end
private
def item_availability
if quantity.to_f > item.quantity.to_f
errors.add(:base, "Not enough quantity for #{item.name} only #{item.quantity} left")
false
end
end
,我有一项服务会触发批准!事件
def run
booking.transaction do
booking.approve!
booking.item.decrement!(:quantity, booking.quantity)
BookingsMailer.send_approval(booking).deliver_now
end
rescue ActiveRecord::RecordInvalid => e
errors.add(:base, e.message)
false
end
这里的问题是,由于数量大于预订的数量,所以验证被命中,但是我服务中的交易不会回滚。
根据文档
保存包括在Job类上运行所有验证。如果 whiny_persistence标志设置为true,在以下情况下引发异常 失败。
我已经将whiny_transitions设置为true,但是