state_machines-activerecord
state_machines-activerecord copied to clipboard
enum support
In an existing model, my state column is a Rails enum and using state_machine on this column generates some warnings (and probably does not work) :
Instance method "processing?" is already defined in #<Module:0x00000007615c68>, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true.
Both Enrollment and its :state machine have defined a different default for "state". Use only one or the other for defining defaults to avoid unexpected behaviors.
I am migrating from aasm and this is a nice feature I miss.
@renchap this is probably a problem for the parent project state_machines.
I think you need to simple remove your enum
declaration, because state_machines is trying to generate the same methods.
state_machine requires a string state
column (you use a different column) and it generates similar methods that you might be expecting from an enum. Perhaps you had to have an enum with aasm and simply don't with state_machine?
@renchap, so, you have integers in your base? You can use something like:
state_machine :status, attribute: :my_integer_status_id, initial: :parked do
state :parked, value: 0 # from enum
state :idling, value: 1 # from enum
event :ignite do
transition :parked => :idling
end
# ...
end
FWIW, I think this adapter could add some nice support for ActiveRecord::Enum that would allow it to coexist. In short, it could detect if there is an existing enum defined for the state machine column, and then:
- Not complain about method conflicts
- Either interface as strings through the ActiveRecord::Enum layer, or else detect and use the integer values directly.
This should effectively allow people to have both an enum and state machine definition for the same column. Now the questions are: Is this worth doing? Are there convincing reasons to keep the enum definition even after adding a state machine? Is the behavior of the shared methods consistent between the two?