Rails AASM gem偶现NoMethodError:事件与状态方法未定义求助
Customer模型AASM状态机偶发未定义方法错误问题
我在Customer模型中基于customer_status列配置了AASM状态机,同时定义了activate事件,模型代码如下:
class Customer < ApplicationRecord include AASM aasm column: :customer_status, whiny_transitions: true do state :active state :pending state :inactive state :cancelled event :activate do transitions from: :pending, to: :active end end enum customer_status: { active: "active", pending: "pending", inactive: "inactive", cancelled: "cancelled" } end
但偶尔调用customer.activate(args_here)时会报错:
NoMethodError: undefined method 'activate' for #<Customer id: .........>
类似地,customer.cancelled?这类状态相关的判断方法也极少出现相同的未定义方法错误。大部分情况下状态机功能正常,仅少数场景触发该问题,我未在AASM官方仓库找到相关问题记录,疑惑该问题的触发原因。
补充说明:
- 未使用STI单表继承
customer_status字段在PostgreSQL中为character varying类型- 模型中同时定义了对应字段的枚举
内容的提问来源于stack exchange,提问作者panagspirou
相关产品推荐
相关产品推荐

