Hello again Rails community!
first of all thanks for your time reading this. I apologize in case this appeared before, I swear I have tried a lot of combinations in the search above, found nothing.
Currently
Enum allows us to map the values to integers in the database. The typical use case would be something like:
class CreateOrders < ActiveRecord::Migration[5.2]
def change
create_table :orders do |t|
t.integer :status
end
end
end
class Order < ApplicationRecord
enum status: [:ok, :billing, :failed]
end
This would map the following:
Order.statuses[:ok] # => 0
Order.statuses[:billing] # => 1
Order.statuses[:failed] # => 2
The problem with integers is that they don't read well in the database. Seeing status = 0 isn't meaningful, so at the end lots of teams, including us, end having a string in the database and mapping enums with a hash.
class CreateOrders < ActiveRecord::Migration[5.2]
def change
create_table :orders do |t|
t.string :status
end
end
end
class Order < ApplicationRecord
enum status: { ok: 'ok', billing: 'billing', failed: 'failed' }
end
Writing this really feels to us like unnecessary boilerplate. I would like to write a PR to allow having a string in the database and declaring the enum with an array of symbols.
class CreateOrders < ActiveRecord::Migration[5.2]
def change
create_table :orders do |t|
t.string :status
end
end
end
class Order < ApplicationRecord
enum status: [:ok, :billing, :failed]
end
I think this would feel much better, cleaner. Of course I will keep current options and syntax. Do you like it?