LoginSignup
1
0

生まれたてのuserマイグレーションファイルをじっくり見てみた

Last updated at Posted at 2024-04-26

ユーザー管理機能の実装時に用いられるdeviseライブラリ。
それによって生成できるuserモデルと勝手に爆誕するuserマイグレーションファイル。

今回はモデル生成により勝手に下界に君臨してしまったuserマイグレーションファイルについて、暇なので観察することにする。

特に今回は、生まれたての状態時を見ていく。

userマイグレーションファイル爆誕

rails g model userと手慣れたようにコマンドを打ち込むと、以下のとおりマイグレーションファイルが勝手にできる。(事前にdeviseのインストールはしておくんだお)

db/migrate/xxxxxxxxxx_devise_create_users.rb

記述は以下のとおり

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

おそらく初学者はemailpasswordのカラムは勝手にできることは把握しているだろう。しかしそれより下の記述を見たことがあるであろうか。

私は特に、以下の2つが気になる。

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at
    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true

今日はこの2つの記述について知ろう。

RecoverableとRememberable

Recoverable

reset_password_token
これは、パスワードリセット機能を実装するために使用されるトークンを保存するカラムである。
ユーザーがパスワードを忘れた場合、このトークンを用いてパスワードリセットプロセスを識別し、ユーザーが新しいパスワードを設定できるようにする。

reset_password_sent_at
ユーザーにパスワードリセットの指示を送信した日時を記録するカラムである。この情報は、パスワードリセットトークンの有効期限の管理などに使用される。

Rememberable

remember_created_at
「ログイン状態を保持する」機能を実現するためのカラムで、ユーザーが「ログインしたままにする」オプションを選択した日時が記録される。Deviseはこの日時を使用して、ユーザーのセッションが長期にわたって有効であるかを判断する。この機能は、クッキーに保存された記憶トークンを用いて、ユーザーがブラウザを閉じても再度訪問した際に自動的にログインさせることができるようにするものである。

add_index ~~~

indexとは和訳すると「索引」である。
add_indexは特定のカラムからデータを取得する際に、テーブルの中の特定のカラムのデータを複製し検索しやすくするための記述である。

ここで疑問。

「特定のカラムとは何か?」
「なぜ検索しやすくするのか?」

それぞれお答えしよう。

特定のカラムとは何か?

ズバリuserテーブルのemailカラムとreset_password_tokenカラムだ。
理由はadd_indexの直後の記述が以下だからである。

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true

なぜ検索しやすくするのか?

ズバリ 一意であるか(オンリーワン) を確認するためである。
これはuniqueオプションが定義されているため、読み取ることができる。

最後

いかがだっただろうか。
おそらく初学者は学習を進めるので一杯一杯で、今回のように「生まれたてのファイルには何が書かれているのか?」に着目できないだろう。
もしほんの少し余裕ができた頃にこの記事を見てくれたなら、もしくは勉強で精一杯だけどもっと自己成長したいと思ってみてくれたなら嬉。

1
0
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
1
0