Ruby on Rails - Create a migration in ruby on rail- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Create a migration in ruby on rails
Add/remove fields in existing tables Add/remove fields in existing tables
Create a migration by running:
rails generate migration AddTitleToCategories title: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
This will create a migration that adds a title column to a categories table:
class AddTitleToCategories < ActiveRecord::Migration[5.0]
def change
add_column :categories, :title, :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
Similarly, you can generate a migration to remove a column: rails generate migration RemoveTitleFromCategories title:string This will create a migration that removes a title column from the categories table:
class RemoveTitleFromCategories < ActiveRecord::Migration[5.0]
def change
remove_column :categories, :title, :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
While, strictly speaking, specifying type (:string in this case) is not necessary for removing a column, it's helpful, since it provides the information necessary for rolling it back.
Create a table
Create a migration by running:
rails g CreateUsers name bio
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 detects the intent to create a join table by finding JoinTable in migration name. Everything else is determined from the names of the fields you give after the name.
class CreateJoinTableParticipation < ActiveRecord::Migration
def change
create_join_table :users, :groups do |t|
# t.index [:user_id, :group_id]
# t.index [:group_id, :user_id]
end
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
Uncomment the necessary index statements and delete the rest.
Precedence
Notice that the example migration name CreateJoinTableParticipation matches the rule for table creation: it has a Create prefix. But it did not generate a simple create_table. This is because migration generator (source code) uses a first match of the following list:
(Add|Remove) (To|From)<table_name>JoinTable - Create<table_name>