Skip to content

Invoca/activerecord-mysql-enum

Repository files navigation

ActiveRecord::Mysql::Enum

This gem is an extension to ActiveRecord which enables native support of enumerations in the database schema using the ENUM type in MySQL. Forked and revitalized from enum_column3 which was itself a fork of a fork of Nick Pohodnya's original gem for Rails 3, enum_column3.

Support

Currently this has been manually tested with Rails version 4 and 5, and works with scaffolding.

Supported adapters:

  • mysql
  • mysql2
  • jdbcmysql (by Nilesh Trivedi)

Installation

In your Gemfile add the following snippet

gem 'activerecord-mysql-enum', '~> 0.1', require: 'active_record/mysql/enum'

Usage

Schema Definitions

When defining an enum in your schema, specify the constraint as a limit:

create_table :enumerations, :force => true do |t|
  t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium
  t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
end

Model Validations

You can then automatically validate this column using:

validates_columns :severity, :color

Setting/Getting Values

All enumerated values will be given as symbols.

@e = Enumeration.new
@e.severity = :medium

You can always use the column reflection to get the list of possible values from the database column.

irb(1)> Enumeration.columns_hash['color'].limit
=> [:red, :blue, :green, :yellow]
irb(2)> @enumeration.column_for_attribute(:color).limit
=> [:red, :blue, :green, :yellow]

Form Helpers

You can use enum_select helper to generate input for enumerated attribute as:

<%= enum_select(@enumeration, 'severity')%>

Or using form_for:

<%= form_for @enumeration do |f| %>
  <%= f.label :severity %>
  <%= f.enum_select :severity %>
<% end %>

About

native MySQL enum column support for ActiveRecord

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 88.0%
  • HTML 9.4%
  • CSS 1.3%
  • JavaScript 1.3%