state-machines / state_machines-activerecord

StateMachines Active Record Integration
https://github.com/state-machines/state_machines-activerecord
MIT License
401 stars 83 forks source link

State based method gives error on transition callbacks with syntax "after_transition <event/transition>, do: [:method]" #86

Closed codemilan closed 4 years ago

codemilan commented 4 years ago

From below code :

# On doing "Question.create().fire_answered"

class Question < ApplicationRecord
  state_machine initial: :created do
    # this callback definition gives error as:
    #  "ArgumentError: wrong number of arguments (given 1, expected 0)" from line 'def analyze'.
    after_transition on: :fire_answered, do: [:analyze] 

    # this callback definition works.
    # after_transition on: :fire_answered { |q| q.analyze }

    # if 'analyze' is not state based method than both works.

    event :fire_answered do
      transition from: :created, to: :answered
    end

    state :answered do
        validates_presence_of :response_time

        def analyze
          puts '*** Inside analyze ***'
        end
     end
  end
end
seuros commented 4 years ago
 def analyze(t)
          puts '*** Inside analyze ***'
 end

You method need to accept the transition.

Let me know if that fixed your issue.

codemilan commented 4 years ago

@seuros yes it worked. Thank you.