rails g migration add_status_to_catalogs status:catalog_status
class AddStatusToCatalogs < ActiveRecord::Migration[5.1]
def up
execute <<-SQL
CREATE TYPE catalog_status AS ENUM ('published', 'unpublished', 'not_set');
SQL
add_column :catalogs, :status, :catalog_status
add_index :catalogs, :status
end
def down
remove_column :catalogs, :status
execute <<-SQL
DROP TYPE catalog_status;
SQL
end
end
class CatalogStatus
STATUSES = %w(published unpublished not_set).freeze
def initialize(status)
@status = status
end
# what you need here
end
class Catalog
enum status: array_to_enum_hash(CatalogStatus::STATUSES), _suffix: true
def status
@status ||= CatalogStatus.new(read_attribute(:status))
end
end
def array_to_enum_hash(a)
a.reduce({}) {|acc, x| acc.update(x.to_sym => x.to_s)}
end
This seems to be the final solution from this Blog: https://naturaily.com/blog/ruby-on-rails-enum
The write-up was helpful, in my option, as it breaks down all of the components.
However there is no explanation of the array_to_enum_hash method in the blog.
id recommend: