I have an Ash state machine defined for a resource, which I want to use to ensure that users change the status to one of the allowed in the business process model. The state machine works fine when using it with my create action. However, when I try to use it with my update action, it gives me an error in the debug messages. The error message doesn’t prohibit me from using the form, and when updating the form it actually works like it’s supposed to. I’m not sure if I’m missing something or if it’s a small bug? My question is: How do I fix this error message without allowing nil as an extra_state? The reason I want to avoid the extra_state is because it violates the attribute constraint.
The state machine definition:
@valid_states [:state_a, :state_b, :state_c, :state_d]
@closed_states [:state_c, :state_d]
state_machine do
initial_states(@valid_states)
default_initial_state(:state_a)
state_attribute(:status)
transitions do
transition(:*, from: :state_a, to: @valid_states)
transition(:*, from: :state_b, to: @valid_states)
transition(:*, from: :state_c, to: @closed_states)
transition(:*, from: :state_d, to: @closed_states)
end
The attribute in question
attribute :status, :atom do
constraints one_of: @valid_states
default :state_a
allow_nil? false
public? true
end
How I create the form:
form =
case
|> Form.for_update(:update, forms: [auto?: true])
|> to_form()
The error message:
[error] Attempted to transition to an unknown state.
This usually means that one of the following is true:
* You have a missing transition definition in your state machine
To remediate this, add a transition.
* You are using `:*` to include a state that appears nowhere in the state machine definition
To remediate this, add the `extra_states` option and include the state nil