2016-07-31 2 views
2

У меня есть модель, называемая запросом организации. Я поместил файлы stateman на эту модель.Rails 4 - Statesman - нет ошибки метода

У меня есть несколько других моделей, которые я также добавил государственного деятеля, и они отлично работают.

Когда я пытаюсь использовать эту государственную машину, я получаю ошибки, которые говорят:

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> 

Может кто-нибудь, почему?

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 

ответ

0

Я думаю, что вы не прошли association_name в функцию. Дайте ему попробовать (не проверено):

def state_machine 
    @state_machine ||= OrderStateMachine.new(self, transition_class: OrderTransition, association_name: :organisation_request_transitions) 
end 
+0

@Maverick_Tan - спасибо за предложение. Я попробовал (хотя я не использую это в моделях других государственных машин). Это не сработало - я все еще получаю сообщение об ошибке: NoMethodError: undefined method 'current_state 'для # Mel

+0

Я также пробовал: имя_соединения:: переходы - но я получаю ту же ошибку – Mel

0

Я думаю, что в соответствии с stateman gem он должен быть

<object>.state_machine.current_state 

Eg

o = OrganisationRequest.last 
o.state_machine.current_state 

не

o.current_state

НТН

+0

Спасибо @ sameera207 - проблема в том, что я неправильно настроил ассоциацию. Большое спасибо за попытку помочь. Для других я добавил ответ выше. – Mel

1

Существовали две ошибки в моей установке:

Первое, что я неправильно назвал имя класса в качестве переменной в имеет много ассоциации - это должно быть:

has_many :transitions, class_name: 'OrganisationRequestTransition', autosave: false 

Второй заключалось в том, что у меня были обратные вызовы after_transition в модели запроса организации - они должны находиться в конечной машине запроса организации.