How to generate a model with a enum type field?
enums, ruby-on-rails
Solution
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.
Problem
I want to generate a model and the corresponding database table in Rails using the generator script. The database table has a field with "enum" type. How can I generate it? The table’s definition in SQL: ``` create table works { id int unsigned not null auto_increment, nickname varchar(20) not null, sex enum('m', 'f') not null }; ``` The Rails generator command: ``` script/generator work nickname:string sex:(what should I write here?) ```