Skip to content

Specifying a Default State

CodeMeister edited this page Feb 28, 2023 · 1 revision

with a default state:

By default, the status of a new instance is set to the first defined state. This can be changed with the default option.

standard default example:

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft
    state :pending
    state :published
    state :archived
  end
end

Post.new.status #=> 'draft'

specified default example:

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft
    state :pending
    state :published
    state :archived

    default :published
  end
end

Post.new.status #=> 'published'

Clone this wiki locally