← Back to list

Fixing “NameError (undefined local variable or method ‘to_adapter’ for class User)” In Rails 8 with…

Recently, I ran into a strange error while working on a Rails 8 API with Devise and DeviseTokenAuth:

Samuel Opoku Asare · 2025-08-17 00:26 · 7 claps · 1.1 min read
#ruby-on-rails #ruby #rails #devise
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 🌐 · Web Development

Fixing “NameError (undefined local variable or method ‘to_adapter’ for class User)” In Rails 8 with DeviseTokenAuth

Recently, I ran into a strange error while working on a Rails 8 API with Devise and DeviseTokenAuth:

NameError (undefined local variable or method `to_adapter' for class User)

At first glance, it looked like something was wrong with my User model or my database. But the real issue turned out to be much simpler.

The Setup

I had a pretty standard User model:

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

And I mounted DeviseTokenAuth in my routes:

mount_devise_token_auth_for 'User', at: 'auth'

The Cause

Devise uses an internal method called to_adapter to talk to the database. That method comes from the Devise ActiveRecord adapter (devise/orm/active_record).

In older setups, Devise used to load this adapter automatically. But in Rails 8, with newer Devise versions, the auto-require doesn’t happen anymore. If the adapter isn’t loaded, your model won’t respond to to_adapter, and Devise crashes.

The Fix

Add this line to your config/initializers/devise.rb:

Devise.setup do |config|
  require 'devise/orm/active_record'
end

Then restart your Rails server. It should work now.

Why This Matters?

The tricky part is that this error might not show up right away. Sometimes another gem or Rails reload sequence might load the adapter for you, making it seem like everything works… until it doesn’t.

By explicitly requiring the adapter, you’re making your app robust against these load order quirks.


메타데이터
post_id
2aa3d9fdbb92
slug
fixing-nameerror-undefined-local-variable-or-method-to-adapter-for-class-user-in-rails-8-with-2aa3d9fdbb92
url
https://medium.com/@asare11samuel/fixing-nameerror-undefined-local-variable-or-method-to-adapter-for-class-user-in-rails-8-with-2aa3d9fdbb92
canonical_url
https://medium.com/@asare11samuel/fixing-nameerror-undefined-local-variable-or-method-to-adapter-for-class-user-in-rails-8-with-2aa3d9fdbb92
author_url
https://medium.com/@asare11samuel
status
ok
fetched_at
2026-07-18 04:57:09