LoginSignup
15
17

More than 5 years have passed since last update.

rails カラムの複数追加

Last updated at Posted at 2018-12-18

$ rails generate migration add_カラム名Toテーブル名 カラム名:データ型 カラム名:データ型
(例)
$ rails generate migration add_basic_info_to_users basic_time:datetime work_time:datetime

$ rails db:migrate
$ rails db:migrate:reset

20181218130615_add_basic_time_to_users.rb
class AddBasicTimeToUsers < ActiveRecord::Migration[5.1]
  def change
    add_column :users, :basic_time, :datetime
    add_column :users, :specified_working_time, :datetime
    add_column :users, :affiliation, :string #ここに追加したいカラムを指定
  end
end

$ rails db:migrate

ちなみに、以下コマンドのようにカラムを複数追加することもできます。Detailsの箇所はどんな名前でもOKです。

postsテーブルにstring型のtitle項目とauthor項目を追加する
$ rails generate migration add_basic_info_to_users basic_time:datetime work_time:datetime

$ rails db:migrate

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17