Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,20 @@ end

You could also use a calculated column or view in your database.

Also if you wish to prevent the state column from going into an asynchronous values with the state machine you can force it to sync with rails validates. In your model file:

```ruby
enum state: OrderStateMachine.states.collect { |s| [s,s]}.to_h # It is recommended to use string enum.
validates :state, inclusion: { in: states.values }, allow_nil: true
define_method(:set_initial_state) { self.state = self.current_state if self.state.nil? }
after_initialize :set_initial_state, if: :new_record?
validate :state_in_sync?
private
def state_in_sync?
errors.add(:state, :invalid, message: 'is not equal current_state', strict: false) unless state == current_state
end
```

### Accessing metadata from the last transition

Given a field `foo` that was stored in the metadata, you can access it like so:
Expand Down