Rails state_machine: How to trigger an action: initial state?

This is what I have:

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

Now I would like to call: send_trade_notification_email in the open state.

How to do it?

Greetings

Joel

+5
source share
2 answers

Since state transitions are similar to “regular” AR callbacks, maybe your method should run in the after_create callback? In the end, your transition from zero to discover what happens when you create:

 after_create :send_trade_notification_email

What if it were allowed to do the same thing:

 after_transition :on=>:create, :do=>:send_trade_notification_email
+3
source

I found a different approach

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
0
source

All Articles