Ruby-on-rails – Rails – generate Scaffold with Table Options

rubyruby-on-rails

I've started learning Ruby last week, and I've started learning Rails today, so bear with me.
I see that Rails come with generators that allow you to generate models/controllers or even model+controller+views bundle as 'scaffold'. This is what I am interested in.

However, I have a question. How do I set database options of the column?

eg. To generate a users table I would do:

rails g scaffold users uuid:binary name:string email:string password:binary registered_on:date number:integer default:string

Now what if I'm integrity freak and am not happy by having validation just in model/controller, but want to do database level limitations as well.
What if I want email to be 50 characters max, and number to Auto-Increment and neither of all fields is allowed to be NULL and default field must have a default of 'foo'. Is there any way to pass these requirements to generator command?

I know its possible to set these options in .rb file that is used in rake db:migrate, however it would be easier to just pass values in with 1 command.

Best Answer

At least some things are available, like string length, but not sure about the others.

rails g scaffold users email:string{50}
Related Topic