Rails aasm gem: "NoMethodError: undefined method" for event method

55 views Asked by At

Inside my Customer model I setup AASM on customer_status column, setting up an activate event too.

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

However, sporadically then the activate aasm call, i.e customer.activate(args_here), may fail with: NoMethodError: undefined method 'activate' for #<Customer id: .........>

In case it matters, I have got same kind of error, again, sporadically and rarely, for status-related aasm methods. Eg. customer.cancelled? have failed with same error:

NoMethodError: undefined method `cancelled?' for #<Customer id: 

Does anyone have any idea what this is about? Could not find a related issue on aasm gem.

I would expect these aasm state-related and event-related methods to be available on the Customer object or at least be consistent. Why in the huge majority of cases no error is raised and in a tiny minority the error is raised, does not make sense to me.

EDIT: Answering to the 1st question: No STI is used, and customer_status field is 'character varying' in db (postgresql) and is defined as enum in Customer model, as such:

enum customer_status: {:active=>"active", :pending=>"pending", :inactive=>"inactive", :cancelled=>"cancelled"}

Also, keep in mind that this fails, but rarely, not all times, i.e works most of the time.

0

There are 0 answers