Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I describe an enumeration column in a Rails 3 migration?

How do I describe an enumeration column in a Rails 3 migration?

like image 328
Zeck Avatar asked Nov 26 '10 05:11

Zeck


People also ask

How does enum work in Rails?

In Ruby on Rails, an enum is an attribute where the values map to integers in the database and can be queried by name. For example, we could define an enum for the status attribute, where the possible values are pending , active , or archived . Ruby on Rails added support for enums in Rails 4.1.

What is enum variable?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

How does Rails keep track of migrations?

Every time a migration is generated using the rails g migration command, Rails generates the migration file with a unique timestamp. The timestamp is in the format YYYYMMDDHHMMSS . Whenever a migration is run, Rails inserts the migration timestamp into an internal table schema_migrations .

How do I create an enum in SQL?

In MySQL one can create an enum as such: USE WorldofWarcraft; CREATE TABLE [users] ( ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, username varchar(255), password varchar(255), mail varchar (255), rank ENUM ('Fresh meat', 'Intern','Janitor','Lieutenant','Supreme being')DEFAULT 'Fresh meat', );


1 Answers

Rails 4.1 contains enum for now!

You can write just

class User < ActiveRecord::Base   enum status: [ :admin, :user, :banned ] end 

For migration write

t.integer :status 

Rails 3 & 4.0

Best solution in my opinion is simple_enum gem.

like image 90
asiniy Avatar answered Oct 09 '22 03:10

asiniy