← Back to list

Fixing undefined method 'devise' When Using devise_token_auth in Rails 8

While working on a Rails 8 project that used Devise together with devise_token_auth, I ran into a frustrating error when running commands…

Samuel Opoku Asare · 2025-08-16 00:30 · 0 claps · 1.1 min read
#ruby-on-rails #devise #rails
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 🌐 · Web Development 🥊 · Combat Sports 🏃 · Running & Endurance

Fixing undefined method 'devise' When Using devise_token_auth in Rails 8

While working on a Rails 8 project that used Devise together with devise_token_auth, I ran into a frustrating error when running commands like:

rails routes

The error looked like this:

....undefined method `devise' for class MyModel (NoMethodError)....

My model was set up the way devise_token_auth’s docs recommend (it was infact auto-generated):

class MyModel < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User
end

Why this happens?

When you call mount_devise_token_auth_for in your routes, devise_token_auth immediately loads the model you pass to it.

Normally, Devise has already extended ActiveRecord with its devise method via:

ActiveSupport.on_load(:active_record) do
  extend Devise::Models
end

But in Rails 8, lazy loading means devise_token_auth can load your model before Devise’s extension runs. When that happens, the devise method doesn’t exist yet, and you get the undefined method 'devise' error.

A quick work around?

Manually extend Devise’s model helpers inside your model like so:

class MyModel < ApplicationRecord
  extend Devise::Models
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User
end

This ensures the devise macro is defined right away, even if devise_token_auth loads the model before Devise’s initializer runs.

Takeaway

If you’re using devise_token_auth with Rails 8 and see undefined method 'devise', it’s likely a load-order issue, not a missing gem.

Adding extend Devise::Models to the model is a simple workaround until Devise (or devise_token_auth) updates to handle Rails 8’s lazy loading more gracefully.


메타데이터
post_id
eab283b1e3a9
slug
fixing-undefined-method-devise-when-using-devise-token-auth-in-rails-8-eab283b1e3a9
url
https://medium.com/@asare11samuel/fixing-undefined-method-devise-when-using-devise-token-auth-in-rails-8-eab283b1e3a9
canonical_url
https://medium.com/@asare11samuel/fixing-undefined-method-devise-when-using-devise-token-auth-in-rails-8-eab283b1e3a9
author_url
https://medium.com/@asare11samuel
status
ok
fetched_at
2026-07-18 05:07:49