Reputation: 2683
I have a model called organisation request. I have put statesman files on this model.
I have a couple of other models which I have also added statesman to and those work fine.
When I try to use this state machine, I get errors which say:
o = OrganisationRequest.last
OrganisationRequest Load (5.6ms) SELECT "organisation_requests".* FROM "organisation_requests" ORDER BY "organisation_requests"."id" DESC LIMIT 1
=> #<OrganisationRequest id: 1, profile_id: 40, organisation_id: 1, created_at: "2016-07-30 21:38:25", updated_at: "2016-07-30 21:38:25">
2.3.0p0 :137 > o.current_state
NoMethodError: undefined method `current_state' for #<OrganisationRequest:0x007f920bb21110>
Can anyone see why?
class OrganisationRequest < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordQueries
# --------------- associations
belongs_to :profile
belongs_to :organisation
has_many :organisation_request_transitions, autosave: false
# --------------- scopes
# --------------- validations
# --------------- class methods
def state_machine
@state_machine ||= OrganisationRequestStateMachine.new(self, transition_class: OrganisationRequestTransition)
end
delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
to: :state_machine
# --------------- callbacks
OrganisationRequest.after_transition(from: :requested, to: :approved) do |organisation_request, profile|
profile.organisation_id.update_attributes!(organisation_id: matching_organisation.id)
# add a mailer to send to the user that is added to the org
end
OrganisationRequest.after_transition(from: :approved, to: :removed) do |organisation_request, profile|
profile.organisation_id.update_attributes!(organisation_id: nil)
end
# --------------- instance methods
# --------------- private methods
private
def self.transition_class
OrganisationRequestTransition
end
def self.initial_state
:requested
end
end
Upvotes: 2
Views: 278
Reputation: 2683
There were two errors in my setup:
The first was that I incorrectly named the class name as a variable in the has many association - it should be:
has_many :transitions, class_name: 'OrganisationRequestTransition', autosave: false
The second was that I had the after_transition callbacks in the organisation request model - they should be in the organisation request state machine.
Upvotes: 1
Reputation: 16629
I think as per the stateman gem it should be
<object>.state_machine.current_state
Eg
o = OrganisationRequest.last
o.state_machine.current_state
not
o.current_state
HTH
Upvotes: 0
Reputation: 3376
I think you didn't pass association_name
into the function. Give it a try (not tested):
def state_machine
@state_machine ||= OrderStateMachine.new(self, transition_class: OrderTransition, association_name: :organisation_request_transitions)
end
Upvotes: 0