Getting previous states in state_machine rails

I am developing a state machine using state_machine on rails, here I have different states that are defined, depending on the state of the conditions, it will be changed to another state. Here is my question: how to get the previous state? and my second question is how to list all the states in a state machine?

I will explain my question, for example, I will say that I have 3 states state1, state2, state3 will initially be in state1. when event 1 made state changes from state1 => state2, now I wanted to know which is my previous state. kindly help me sort this out.

+4
source share
4 answers

This may be what you are looking for: https://github.com/wvanbergen/state_machine-audit_trail

+1
source

I got stuck in a similar case and found a suitable answer that works for me. Maybe this will help you fooobar.com/questions/1389459 / ...

+2
source

If you are looking for a list of previous states from an object, I am afraid that you cannot. state_machine only changes the value of the column in your register of the model database, does not save any type of history.

Please check for solutions like actions_as_versioned (https://github.com/technoweenie/acts_as_versioned) to store and play back your model versions.

Also you want to check out aasm pearls https://github.com/rubyist/aasm

Greetings

+1
source

I used paper_trail gem and confirmed previous versions.

This allows me to track the sequence of states through which the object went.

With paper_trail, you can even get the Source or Terminator of this state change.

+1
source

Source: https://habr.com/ru/post/1389457/


All Articles