カテゴリー
サインイン 新規登録

間違いや改善の指摘

内容の技術的な誤り・誤字脱字やミスのご報告・解説やトピックの追記/改善のご要望は教材をさらに良くしていく上でとても貴重なご意見になります。

少しでも気になった点があれば、ご遠慮なく投稿いただけると幸いです🙏

実際には誤りではなく勘違いであっても、ご報告いただけることで教材のブラッシュアップにつながります。

質問ポリシー①

教材受講者みなさんのスムーズな問題解決のために、心がけていただきたいことがあります。

教材の内容に関する質問を投稿しましょう

教材の内容に関係のない質問や教材とは異なる環境・バージョンで進めている場合のエラーなど、教材に関係しない質問は推奨していないため回答できない場合がございます。

その場合、teratailなどの外部サイトを利用して質問することをおすすめします。教材の誤字脱字や追記・改善の要望は「文章の間違いや改善点の指摘」からお願いします。

6-2

自己紹介文のカラムを追加

本パートでは、プロフィールを編集する際に設定できる自己紹介文のカラムを追加します。

本パートのゴール

usersテーブルに自己紹介文のカラムを追加します。

ゴールまでの流れ

  1. usersテーブルの構成を確認
  2. マイグレーションファイルの作成
  3. マイグレーションファイルを実行
  4. schema.rb を確認

では、実際に進めていきましょう。

1. usersテーブルの構成を確認

usersテーブルの構成は以下のようになります。

カラム名 データ型
email string
password string
name string
gender integer
self_introduction text

自己紹介文のカラム名はself_introductionにしました。このカラムには長い文字列が入る可能性があるため、データ型はtextにしています。

2. マイグレーションファイルの作成

新しくカラムを追加する際はマイグレーションファイルを作成します。マイグレーションファイルを作成するには以下のようなコマンドを実行します。

【例】

console
Copied!
rails g migration クラス名 カラム名:型

参考)Active Record マイグレーション - マイグレーションを作成する

今回は以下のコマンドを実行してマイグレーションファイルを作成してください。

console
Copied!
rails g migration AddSelfIntroductionToUser self_introduction:text

実行結果は以下になります。

console
Copied!
rails g migration AddSelfIntroductionToUser self_introduction:text Running via Spring preloader in process 36032 invoke active_record create db/migrate/20201118013611_add_self_introduction_to_user.rb

3. マイグレーションファイルを実行

マイグレーションファイルを作成しただけでは、テーブルに反映されません。マイグレーションファイルをテーブルに反映するにはマイグレーションファイルを実行する必要があります。以下のコマンドで実行できます。

console
Copied!
rails db:migrate

上記のコマンドを実行すると下記のような実行結果が表示されます。

console
Copied!
rails db:migrate == 20201118013611 AddSelfIntroductionToUser: migrating ======================== -- add_column(:users, :self_introduction, :text) -> 0.0112s == 20201118013611 AddSelfIntroductionToUser: migrated (0.0113s) ===============

4. schema.rb を確認

Railsで作成したテーブルの情報は、db/schema.rbというファイルに記載されています。実際に見てみましょう。

db/schema.rb
123456789101112131415161718192021222324252627282930313233
Copied!
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_11_18_013611) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "name", null: false t.integer "gender", default: 0, null: false t.text "self_introduction" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end

t.text "self_introduction" というコードがあれば自己紹介文のカラムの追加ができています。

以上で、本パートは終了です。

お疲れさまでした。