schema.rbを見る
db/schema.rb
を見ると視覚的にテーブルとカラム構造を確認できる。
db/schema.rb
ActiveRecord::Schema.define(version: 2020_07_04_034908) do
enable_extension "plpgsql"
create_table "tasks", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "uuid"
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
schema.rbとは
schema.rb
はRailsによって自動的に作られるファイルで、現在のデータベースの状態が書かれている。
データベースに変更を加えた時、すなわちrails db:migrate
やrails db:rollback
をした時に自動的に書き変えられる。
annotateを使おう!
いちいちschema.rb
を開くのは億劫なのでannotateというgemを使うと楽。annotateはテーブルを更新するたびに自動でテーブルの構造をモデルに書いてくれる。
このようにモデルにコメントとして書かれる。
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# address :string
# email :string default(""), not null
# encrypted_password :string default(""), not null
# introduction :text
# name :string
# postal_code :integer
# provider :string default(""), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# uid :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_uid_and_provider (uid,provider) UNIQUE