Adding multiple columns to a table

To add multiple columns to a table, separate field:type pairs with spaces when using rails generate migration command.

The general syntax is:

rails generate migration NAME [field[:type][:index] field[:type][:index]] [options]

For example, the following will add name, salary and email fields to the users table:

rails generate migration AddDetailsToUsers name:string salary:decimal email:string

Which generates the following migration:

class AddDetailsToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :name, :string
    add_column :users, :salary, :decimal
    add_column :users, :email, :string
  end
end

results matching ""

    No results matching ""