Handle integer-backed state attributes
masterThe gem provides two ways to handle integer columns used for states:
Automatic Conversion (Default)
If states do not declare explicit integer values, the gem converts them transparently. Application code reads state names (strings), but the database stores integers (mapped by definition order).
# Database stores 1, but code sees "approved"
order.status = :approved
order.status # => "approved"Explicit Integer Values
If states declare explicit values, the gem maintains classic raw-integer behavior. Reading the attribute returns the integer, and status_name returns the symbol.
# state :pending, value: 0
# state :approved, value: 1
order.status # => 1
order.status_name # => :approvedDisabling Auto-Conversion
To disable all type conversion and use standard ActiveRecord integer handling, set this in an initializer before defining state machines:
# config/initializers/state_machines.rb
StateMachines::Integrations::ActiveRecord.auto_convert_integer_state_attributes = falseclass Order < ApplicationRecord
state_machine :status, initial: :pending do
state :pending
state :approved
end
end
order = Order.create!
order.status = :approved
order.status # => "approved"
# The database stores 1.