Ruby on Rails - rails generate migration in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Rails Generate Migration
- we can generate a rails migration file from the terminal using the following command:
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 a list of all the options supported by the command, we could run the command without any arguments as in rails generate migration.
- For example, if we want to add first_name and last_name fields to users table, you can do:
rails generate migration AddNamesToUsers last_name:string first_name: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
- Rails will create the following migration file:
class AddNamesToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :last_name, :string
add_column :users, :first_name, :string
end
end
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
- Now, apply the pending migrations to the database by running the following in the terminal:
rake db:migrate
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
rails db:migrate