rails state_machine:如何触发操作:初始状态?

时间:2012-01-17 23:21:20

标签: ruby-on-rails state-machine

这就是我所拥有的:

state_machine :state, :initial => :open do
    after_transition :on => :buy, :do => :send_buy_notification_email
    after_transition :on => :take, :do => :send_take_notification_email
    after_transition :on => :accept, :do => :send_accept_notification_email
    after_transition :on => :cancel, :do => :send_cancel_notification_email

现在我想在初始状态打开时触发:send_trade_notification_email。

我该怎么做?

干杯,

乔尔

2 个答案:

答案 0 :(得分:3)

由于状态转换类似于“通常的”AR回调,也许你的方法应该在after_create回调中触发?毕竟,你的过渡是从零到开放,这将在创建时发生:

 after_create :send_trade_notification_email

如果允许的话,将完成同样的事情:

 after_transition :on=>:create, :do=>:send_trade_notification_email

答案 1 :(得分:0)

我找到了另一种方法

def initial_send_trade_notification_email
  send_trade_notification_email #do what you need on create
  :open # return initial state
end

state_machine initial: lambda(&:initial_send_trade_notification_email) do
  #...
end