Search code examples
ruby-on-rails

Generate Rails models with mandatory/required fields (i.e not null) from command line


Can anyone please help with generating Rails models with mandatory fields/columns (i.e. NOT NULL)? For example,

$rails generate model Role name:string <???>

What do I need to specify in order to get the "null: false" constraint as shown below?

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      t.string :name, null: false

      t.timestamps
    end
  end
end

Thanks heaps in advance


Solution

  • You can't do that in a generator command. It's quite simple to add null: false to your migration file though.