2

There are two parts to my problem.

  1. how can i define nil as the initial state of any state machine?
  2. how can i transition any state to nil?

if we do something like this then it saves "nil" as value not NULL,

aasm(:aasm_state, column: :status) do
  state :nil, initial: true
  state :state1, :state2, :state3, :state4
  event :change_state do
    transitions from: :state1, to: :state2
  end
end

how can i modify below code to do both or either of the above mentioned things?

aasm(:aasm_state, column: :status) do
  state :state1, :state2, :state3, :state4
  event :change_state do
    transitions from: :state1, to: :state2
  end
end
  • I do not see something like that mentioned in the docs... but I don't think it is something that could work, since `aasm gem` adds various methods/events based on the `state`. So e.g. `job.nil?` would probably create conflict as well as the `job.nil!` would error – alexts Apr 04 '19 at 19:34
  • Why do you want to use `nil` as a valid state? Is there no better name for that state – like `new`, `initial`, `pending`, etc? – spickermann Apr 04 '19 at 20:05
  • @spickermann i need to make any attribute (lets say x) in my model as null if it satisfy a given condition but even if i update any other attribute (lets say y) of the same model then attribute x also gets update with initial state. i want to prevent the updation of attribute x. – shubham saxena Apr 05 '19 at 06:03
  • @shubhamsaxena Did you ever figure this out? I have a similar use case, would love to see your solution. – eben.english Mar 11 '21 at 16:16

0 Answers0