2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AASM 定義した状態を一覧で取得する Rails, Ruby, Active Record

Posted at

AASMはclassの状態遷移を実装するgemです。

インストール

# Gemfile
gem 'aasm'

定義例(Model)

class Hoge < ActiveRecord::Base
  include AASM
  aasm column: :status do
    state :deactivated, initial: true
    state :activated
    # activated -> deactivated
    event :deactivate do
      transitions from: :activated, to: :deactivated
    end
    # deactivated -> activated
    event :activate do
      transitions from: :deactivated, to: :activated
    end
  end

定義したステータスの一覧は下記命令で取得できます。

pry(main)> Hoge.aasm.states.map(&:name)
=> [:deactivated, :activated]
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?