baggiogiacomo
baggiogiacomo

Reputation: 306

Rails: generate scaffold with enum as field

I want to run rails g scaffold command to generate my billing plans table. I want it like this:

id: string
name: string
description: string
status: enum["enabled", "disabled", "coming_soon"]
price: integer

How can I use that generator to accomplish this? I don't know what to put here rails g scaffold BillingPlans name:string description: string price:integer status: <My awesome enum type>

Using:

Upvotes: 1

Views: 1023

Answers (1)

Roman Van Loo
Roman Van Loo

Reputation: 32

To straight up answer your question:

rails g scaffold BillingPlans name:string description: string price:integer status:integer

The enum would be a normal integer field combined with adding enum status: [:enabled, :disabled, :coming_soon] in the BillingPlans model. (Really nice blogpost about this -> https://naturaily.com/blog/ruby-on-rails-enum)

Now how I would personally do this, don't use scaffold. It creates so many files, just simply create a migration and write everything manually. You'll have full control of every line of code this way. Second, I honestly don't know what the upside is of having an enum field instead of a normal string that is validated to be one of the 3 values you mentioned...

Upvotes: 0

Related Questions