政治家宝石与Rails 5-ActiveRecord过渡未发生

时间:2018-07-11 08:46:31

标签: activerecord ruby-on-rails-5

我正在使用Statesmangem跟踪Rails 5中参与者的状态。下面是我的代码实现

class ParticipantStateMachine

 include Statesman::Machine

 state :screening_started, initial: true
 state :screening_inprogress
 state :screening_completed
 state :enrollment_started
 state :enrollment_inprogress
 state :enrollment_completed

 transition from: :screening_started, to: :screening_inprogress
 transition from: :screening_inprogress, to: :screening_completed

end

class Participant < ApplicationRecord
 include Statesman::Adapters::ActiveRecordQueries

 belongs_to :user
 has_many :participant_transitions

 after_create { |record| record.transition_to :screening_started }

 delegate :can_transition_to?, :transition_to!, :transition_to, :current_state, to: :state_machine


 def state_machine
  @state_machine ||= ParticipantStateMachine.new(self, transition_class: ParticipantTransition)
 end

 end

下面是我的participation_transition模型

class ParticipantTransition < ApplicationRecord
 include Statesman::Adapters::ActiveRecordTransition 

 belongs_to :participant, inverse_of: :participant_transitions
 after_destroy :update_most_recent, if: :most_recent?

 private

  def update_most_recent
   last_transition = 
   participant.participant_transitions.order(:sort_key).last
   return unless last_transition.present?
    last_transition.update_column(:most_recent, true)
  end
end

我的问题

当我创建一个新的参与者(由@ participant.state_machine.current_state验证相同)时,参与者的状态将保存在Stateman内存中,但作为ActiveRecord,状态不会通过其模型保存在participant_transitions表中。

是我缺少的东西吗,我什至在lib中将Stateman初始化为ActiveRecord

Statesman.configure do
 storage_adapter(Statesman::Adapters::ActiveRecord)
end

0 个答案:

没有答案