Using Ruby language, I have to store some ruby :symbols
data type in a relational database (let's say just a simple Sqlite (https://www.sqlite.org/), maybe through Sequel's ORM (see: https://github.com/jeremyevans/sequel). So leave aside discussion of database specific features (postgres enum ect.)
In my application, symbols represent the status of a finite state machine (so I have to manage with a sort of enum).
Let's consider the following code (see also: https://github.com/soveran/micromachine):
require 'micromachine'
machine = MicroMachine.new(:new) # Initial state.
# Define the possible transitions for each event.
machine.when(:confirm, :new => :confirmed)
machine.when(:ignore, :new => :ignored)
machine.when(:reset, :confirmed => :new, :ignored => :new)
machine.trigger(:ignore) #=> true
machine.state #=> :ignored
As you can see I have represented different states using symbols:
:new, :ignored, :confirmed
are in a sense a sort of enum
of status (each finite state machine includes a "fixed" set of states).
Now I have to save (persist) machine.state
in a column of a relational database.
What is the suggested way to store states (symbols in terms of ruby data struct) in a db ?
Here are some possible ideas:
A "relational" method utilising a look-up table to map symbols to numbers, and the performing a join operation.
An alternative would be to code an enumeration (embedding the lookup) inside the ruby program; see: How to store and compare :symbols in an ActiveRecord (Ruby on Rails) and: Enums in Ruby
A trivial solution would be to just convert the string to a symbolbefore saving it to the db:
```
machine.state #=> :ignored
machine.state.to_s #=> "ignored"
dataset.insert(:status => machine.state.to_s)
```
But I'm perplexed because ideally I want avoid an explicit symbol <-> integer mapping (when persisting in the db).
Any suggestion / best practice ?
UPDATE: For completeness: "state" can assume a lot (tens and tens) of different values (symbols), not just the few value in the example here proposed.