1

我在 ActiveRecord 中使用pluginaweek 的 state_machine进行测试管理。我通过此调用检索所有可能状态的数组:

irb(main):013:0> Tester.state_machine.states.map &:name
=> [:candidate, :contract_sent, :contract_received, :contract_wont_return, :active, :retired]

现在,我不想使用 meta_search 构建搜索表单来检索具有特定状态的测试人员。我想使用一个选择:

- states = {}; Tester.state_machine.states.map(&:name).each{ |e| states[t(e.to_s)] = e }
= form.input :state_equals, :as => :select, :collection => states

它可以工作,但不是很好,因为我构建了一个新的哈希并翻译了每个元素。此外,翻译必须插入两次,因为我的解决方案不使用state_machine 的 I18n yaml 结构...

有没有类似的方法

Tester.state_machine.states.map(&:human_state_name)

或者

Tester.state_machine.human_states_name.map(&:name)

获得翻译状态?

4

1 回答 1

1

我找到了解决方案。您可以通过以下方式获得 state_mache 状态的翻译:

- states = {}; Tester.state_machine.states.map() { |s| states[s.human_name] = s.name }
= form.input :state_equals, :as => :select, :collection => states
于 2012-05-21T12:08:38.670 回答