Search code examples
ruby-on-railsrails-generate

Rails - generate scaffold for multi select


I used the following method to generate scaffold for platform list in our app:

rails generate scaffold platform name:string url:string country:string

I would like to still use scaffold possibility to add additional attributes, like type and gender, which should be drop downs, or preferably multi-select's.

Is there a way to use rails generate to create such a models?


Solution

  • You have already generated a scaffold for Platform. So you have two options:

    • edit scaffold by hand to add the new fields (recommended)
    • remove scaffold with rails destroy scaffold and then use rails generate again

    Anyway scaffolding doesn't allow you to specify options for a drop-down menu. In any case you have to manually edit the scaffold. Furthermore in Rails migrations you cannot specify ENUM fields, so the best option for gender would be a string field with unitary length (with a validates_inclusion_of in model).

    P.S. Don't use type as a column name because you will probably get strange behaviors.