LoginSignup
2
0

More than 1 year has passed since last update.

undefined method `current_sign_in_at' for #< ~~~ > の解決方法

Last updated at Posted at 2023-01-12

はじめに

こんにちは! @Keichan_15 です!

Deviseを用いて sign_upsign_in を行うと、表題にある undefined method 'current_sign_in_at' forのエラーが出た際の解決方法について、備忘録も兼ねて記載していこうと思います。

尚、こちらのエラーを検索すると下記のような記事や解決方法が出てきます。結論、解決方法として間違いでは無いです。今回の想定としてはdeviseをinstallしてごくごく一般的なモデル作成を行った場合 ex) $ rails g devise User を前提としてお話していきます。
下記の方法でも間違いでは無いですが、マイグレーションファイルを修正してresetを掛けるといったナンセンスなことは面倒なので…ってことですね。

環境

  • ruby 3.1.2
  • Rails 6.1.6.1
  • Yarn 1.22.19
  • devise (4.8.1)

発生している問題

2023-01-13 (2).png

deviseを活用して sign_upsign_in を行った後に、写真のようなエラーが出ます。
current_sign_in_at のメソッドが無いかアプリケーション内の全てのファイルに検索を掛けると、deviseを用いて作成したマイグレーションファイル内に該当メソッドが存在するのが確認できます。

202201 ~~ _devise_create_users.rb
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[6.1]
  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.string :name
      t.text :introduction
      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

## Trackable にまつわる部分はデフォルトでコメントアウトされているはずです。
ではなぜコメントアウトされているはずなのに NoMethodError を吐いてしまうのでしょうか。

解決方法

非常にシンプルです。:trackable のmoduleを コメントアウト or 削除 してあげてください。

user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable, :trackable, <= コレが要らない!!
         :recoverable, :rememberable, :validatable

  validates :name, uniqueness: true, length: {in:2..20}
  validates :introduction, length: {maximum: 50}

  has_many :books, dependent: :destroy

end

これで問題無く解決するはずです。

古いバージョンのdeviseではdefaultで記載されていたパターンがあるみたい。この辺り調べてみましたが、どのバージョンからdefaultでは無くなったのか確認することができませんでした。有識者の方いらっしゃいましたら是非コメントでご指摘頂けると幸いです。

少なくともdevise 4.8.1 ではdefaultのmoduleでは無いことから、もし同様のエラーが出た場合にはまずモデルを確認してみましょう!

余談

current_sign_in_atについて少し調べると色々発掘しました。端的に解説するとtrackableのmoduleは主にログインにまつわるデータ(サインインした時間や回数など)を保存することができます。詳しい内容は今回の記事では割愛しますが、興味があれば下記の記事を是非ご覧ください。

参考記事

おわりに

deviseのバージョンによってtrackableがデフォルトになっている場合・なっていない場合があるので、マイグレーションファイルとモデルファイルの双方をしっかり確認すると良いと思います~~。
それではまた~~:wave:

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