How to generate a model with a enum type field?

Rails 4.1 added ActiveRecord::Enum, which emulates enums using an integer-type column. You can use them as long as you are willing to change the column type to an integer in the database.

To use these enums, put integer in your generate command:

bin/rails generate Work nickname:string sex:integer

Then add a call to enum in the generated model file:

class Work < ActiveRecord::Base
  enum sex: [ :male, :female ]
end

See Enum’s documentation for more details.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)