Ruby on Rails - Adding multiple columns to a table in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
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]
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
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
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
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