LoginSignup
0
0

More than 5 years have passed since last update.

AASMのfromに指定されている値を取得するには(1行で)

Last updated at Posted at 2019-02-07

AASM Gemででこういうのを定義してたとして

class Job
  include AASM

  aasm do
    state :sleeping, :initial => true
    state :running, :cleaning

    event :run do
      transitions :from => :sleeping, :to => :running
    end

    event :clean do
      transitions :from => :running, :to => :cleaning
    end

    event :sleep do
      transitions :from => [:running, :cleaning], :to => :sleeping
    end
  end
end

sleepイベントのfromを取得したいときは、こんな感じでいけます。

Job.aasm.events.detect { |event| event.name == :sleep }.transitions.map(&:from)

AASM
https://github.com/aasm/aasm

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0