Rails 4 - Statesman - 没有方法错误

时间:2016-07-31 08:05:46

标签: ruby-on-rails ruby states

我有一个名为组织请求的模型。我把政治家文件放在这个模型上。

我还有其他一些模型,我还添加了政治家和那些工作正常。

当我尝试使用这个状态机时,我收到的错误是:

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

3 个答案:

答案 0 :(得分:1)

我的设置中有两个错误:

首先是我错误地将类名命名为具有多个关联的变量 - 它应该是:

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

第二个是我在组织请求模型中有after_transition回调 - 它们应该在组织请求状态机中。

答案 1 :(得分:0)

我认为你没有将association_name传递给函数。试一试(未经测试):

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

答案 2 :(得分:0)

我认为根据stateman gem它应该是

<object>.state_machine.current_state

例如

o = OrganisationRequest.last
o.state_machine.current_state

不是

o.current_state

HTH