教材の内容に関係のない質問や教材とは異なる環境・バージョンで進めている場合のエラーなど、教材に関係しない質問は推奨していないため回答できない場合がございます。
その場合、teratailなどの外部サイトを利用して質問することをおすすめします。教材の誤字脱字や追記・改善の要望は「文章の間違いや改善点の指摘」からお願いします。
本パートでは、プロフィールを編集する際に設定できる自己紹介文のカラムを追加します。
usersテーブルに自己紹介文のカラムを追加します。
では、実際に進めていきましょう。
usersテーブルの構成は以下のようになります。
カラム名 | データ型 |
---|---|
string | |
password | string |
name | string |
gender | integer |
self_introduction | text |
自己紹介文のカラム名はself_introduction
にしました。このカラムには長い文字列が入る可能性があるため、データ型はtext
にしています。
新しくカラムを追加する際はマイグレーションファイルを作成します。マイグレーションファイルを作成するには以下のようなコマンドを実行します。
【例】
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
マイグレーションファイルを作成しただけでは、テーブルに反映されません。マイグレーションファイルをテーブルに反映するにはマイグレーションファイルを実行する必要があります。以下のコマンドで実行できます。
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) ===============
Railsで作成したテーブルの情報は、db/schema.rb
というファイルに記載されています。実際に見てみましょう。
db/schema.rb123456789101112131415161718192021222324252627282930313233 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"
というコードがあれば自己紹介文のカラムの追加ができています。
以上で、本パートは終了です。
お疲れさまでした。