Skip to content

Instantly share code, notes, and snippets.

@okabe-yuya
Last active June 9, 2020 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okabe-yuya/9b0ef574b31017a739db1bd542c91712 to your computer and use it in GitHub Desktop.
Save okabe-yuya/9b0ef574b31017a739db1bd542c91712 to your computer and use it in GitHub Desktop.
How to migration in Rails

create

use rails gem

$ rails generate miogration [ClassName]

use bundle in rails gem

$ bundle exec rails generate migration [ClassName]

eg: ClassName -> AddColumnTitlesToUser

update

add cloumn to a table

$ rails generate migration AddTitleToUser title:string

remove column from a table

$ rails generate migration RemoveTitleToUser title:string

migration file

class AddTitleToUser < ActiveRecord::Migration[5.2]
  def change
    :
  end
end

def changeに対する記述

def change
  # カラムを追加
  add_column :table_name, :column_name, :type, options ...
  # カラム名を更新
  rename_column :table, :old_column, :new_column
end

migrationの実行

$ bundle exec rails db:migrate

@okabe-yuya
Copy link
Author

How nice !
My first migration!

$ bundle exec rails generate migration AddLatestStatusTooXXX latest_status:integer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment