Tutorial by Examples

To run a specific migration up or down, use db:migrate:up or db:migrate:down. Up a specific migration: 5.0 rake db:migrate:up VERSION=20090408054555 5.0 rails db:migrate:up VERSION=20090408054555 Down a specific migration: 5.0 rake db:migrate:down VERSION=20090408054555 5.0 ra...
To create a join table between students and courses, run the command: $ rails g migration CreateJoinTableStudentCourse student course This will generate the following migration: class CreateJoinTableStudentCourse < ActiveRecord::Migration[5.0] def change create_join_table :students, ...
To run migrations in the test environment, run this shell command: rake db:migrate RAILS_ENV=test 5.0 Starting in Rails 5.0, you can use rails instead of rake: rails db:migrate RAILS_ENV=test
To add a new column name to the users table, run the command: rails generate migration AddNameToUsers name This generates the following migration: class AddNameToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :name, :string end end When the migration name i...
To add a new indexed column email to the users table, run the command: rails generate migration AddEmailToUsers email:string:index This will generate the following migration: class AddEmailToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :email, :string add_i...
To remove existing column name from users table, run the command: rails generate migration RemoveNameFromUsers name:string This will generate the following migration: class RemoveNameFromUsers < ActiveRecord::Migration[5.0] def change remove_column :users, :name, :string end end ...
To add a reference to a team to the users table, run this command: $ rails generate migration AddTeamRefToUsers team:references This generates the following migration: class AddTeamRefToUsers < ActiveRecord::Migration[5.0] def change add_reference :users, :team, foreign_key: true ...
To create a new users table with the columns name and salary, run the command: rails generate migration CreateUsers name:string salary:decimal This will generate the following migration: class CreateUsers < ActiveRecord::Migration[5.0] def change create_table :users do |t| t.s...
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 ...
Run command: 5.0 rake db:migrate 5.0 rails db:migrate Specifying target version will run the required migrations (up, down, change) until it has reached the specified version. Here, version number is the numerical prefix on the migration's filename. 5.0 rake db:migrate VERSION=2008090...
To rollback the latest migration, either by reverting the change method or by running the down method. Run command: 5.0 rake db:rollback 5.0 rails db:rollback Rollback the last 3 migrations 5.0 rake db:rollback STEP=3 5.0 rails db:rollback STEP=3 STEP provide the number of ...
If you have created a table with some wrong schema, then the easiest way to change the columns and their properties is change_table. Review the following example: change_table :orders do |t| t.remove :ordered_at # removes column ordered_at t.string :skew_number # adds a new column t.index...
To add a new unique column email to users, run the following command: rails generate migration AddEmailToUsers email:string:uniq This will create the following migration: class AddEmailToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :email, :string add_index...
To modify an existing column in Rails with a migration, run the following command: rails g migration change_column_in_table This will create a new migration file in db/migration directory (if it doesn’t exist already), which will contain the file prefixed with timestamp and migration file name w...
You can rollback and then migrate again using the redo command. This is basically a shortcut that combines rollback and migrate tasks. Run command: 5.0 rake db:migrate:redo 5.0 rails db:migrate:redo You can use the STEP parameter to go back more than one version. For example, to go ba...
The following example adds a column admin to the users table, and gives that column the default value false. class AddDetailsToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :admin, :boolean, default: false end end Migrations with defaults might take a long tim...
To forbid null values in your table columns, add the :null parameter to your migration, like this: class AddPriceToProducts < ActiveRecord::Migration def change add_column :products, :float, null: false end end
We can check the status of migrations by running 3.05.0 rake db:migrate:status 5.0 rails db:migrate:status The output will look like this: Status Migration ID Migration Name -------------------------------------------------- up 20140711185212 Create documentation pages up ...
Hstore columns can be useful to store settings. They are available in PostgreSQL databases after you enabled the extension. class CreatePages < ActiveRecord::Migration[5.0] def change create_table :pages do |t| enable_extension 'hstore' unless extension_enabled?('hstore') t...
A self reference can be useful to build a hierarchical tree. This can be achieved with add_reference in a migration. class AddParentPages < ActiveRecord::Migration[5.0] def change add_reference :pages, :pages end end The foreign key column will be pages_id. If you want to decide a...

Page 1 of 2