如何访问特定rails模型的acts_as_state_machine状态集合?

时间:2009-10-01 22:44:57

标签: ruby-on-rails state-machine aasm

是否可以访问给定模型的状态集合:

班级对话     包括AASM

aasm_initial_state :unread

aasm_state :unread
aasm_state :read
aasm_state :closed

aasm_event :view do
  transitions :to => :read, :from => [:unread]
end

aasm_event :close do
  transitions :to => :closed, :from => [:read, :unread]
end

我希望能得到一系列状态,如:

['unread', 'read', 'closed']

这可能吗?

1 个答案:

答案 0 :(得分:1)

AASM gem有两个类方法,它们返回给定模型的状态集合:

  aasm_states
  aasm_states_for_select

例如:

class Note < ActiveRecord::Base
  aasm_initial_state :unread

  aasm_state :unread
  aasm_state :read
  aasm_state :closed

  aasm_event :view do
    transitions :to => :read, :from => [:unread]
  end

  aasm_event :close do
    transitions :to => :closed, :from => [:read, :unread]
  end 
end

> Note.aasm_states
> Note.aasm_states_for_select