Search code examples
ruby-on-railsscaffolding

Is it possible to assign default values to attributes when using rails scaffold command? How?


For example, I want to generate a scaffold with a boolean field that defaults to false


Solution

  • No, you can not do that from command line.

    You can read about available modifiers to pass from command line in docs.

    Also, very nice practical (a bit old but still useful) article.

    You can do at least the following stuff:

    # Specify the type of the field
    rails g model user email age:integer
    # Add index for column
    rails g model user email:index location_id:integer:index
    # Add uniq index for column
    rails g model user pseudo:string:uniq
    # Set limit for field of integer, string, text and binary fields:
    rails generate model user pseudo:string{30}
    # generate decimal field with scale and precision:
    rails generate model product 'price:decimal{10,2}'
    # You can combine any single curly brace option with the index options:
    rails generate model user username:string{30}:uniq
    # options to generate reference columns (fields which are used in rails as foreign keys):
    rails generate model photo album:references