You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 10:24:54