0

I have a state_machine with three possible states state1, state2, state3.

Whenever an object enter to state_3 I want to trigger an event event1.

in my state_machine, I have

      after_transition :on => :state_3, :do => :event1.

But what happens is whenever the state changes from state1 to state3 or state2 to state3, the event is triggered properly. But when the state transition is from state3 to state3, the event1 is not triggered. How can I achieve this?

I know state3 to state3 is not a state_transition. I can use something like after_save callback and check the state is state3. But I want to know is there an effective way to achieve this is in state_machine?

The reason I want to trigger the event on state3 to state3 is because, I have a form. The form reaches state3 only all questions are answered. After all questions are answered i am calculating the mark. What if the user just changes the answer?. The state will just get update from state3 to state3. This is the case i want to trigger the event from state3 to state3.

Suganya
  • 701
  • 1
  • 9
  • 27
  • 1
    Surely state3 to state3 is not a transition as the state hasn't changed. – ReggieB Mar 25 '15 at 09:37
  • Edited my question. Please go through – Suganya Mar 25 '15 at 09:39
  • But why you want to do `state3` to `state3`, if any specific reason we can try it with different way – Sonalkumar sute Mar 25 '15 at 09:49
  • Edited my question. Please go through – Suganya Mar 25 '15 at 10:07
  • 1
    In UML, what you're describing is known as a "self transition" (from state 3 to state 3). And self transitions _do_ perform the entry (and exit) actions associated with the state. So your design can be described in UML. I'm not familiar with ruby-on-rails so I have no idea whether this information helps. – kkrambo Mar 25 '15 at 11:44
  • @kkrambo ruby has a number of state machine gems, and I expect how they behave on "self translations" depends on which gem is used. https://www.ruby-toolbox.com/categories/state_machines.html – ReggieB Mar 25 '15 at 12:11
  • Using 'state_machine' gem – Suganya Mar 25 '15 at 13:22

1 Answers1

0

When a user changes their answers, they are in affect rerunning the state 2 to 3 transition. So change the state to state2 before applying the changes.

Alternatively, create an updating state and put the questions into that state before applying the changes.

ReggieB
  • 8,100
  • 3
  • 38
  • 46