ALL ARTICLES
SHARE

Rails & Devise: X is not a subclass of User

Bryan Villafañe
Development
4 min read
not a subclass of user

Our team recently ran into a maddening issue with Ruby on Rails, the Devise gem, and Single Table Inheritance (STI). Eventually, the issue got so bad that every single backend change triggered the exception ActiveRecord::SubclassNotFound with the message Invalid single-table inheritance type: ProjectManager is not a subclass of User. The problem was, ProjectManager was a subclass of User. The two classes shared a database table called “users” and ProjectManager inherited from User within the codebase. As it turns out, the fact that ProjectManager used STI and inherited it from User was the core of the issue.

Single Table Inheritance and Rails Autoloading

After some investigation, we figured out that autoloading in Ruby on Rails doesn’t play well with STI. The fact that single table inheritance doesn’t work well with auto-loading is actually documented in the Rails documentation. However, the solution that the Rails documentation provides for dealing with autoloading issues related to STI didn’t work for us, so we had to come up with our own.

Here is a partial stack trace from our Rails app showing the inheritance issue:

Why this Single Table Inheritance Issue Occurs

This bug took us quite a bit to figure out. We actually ended up with breakpoints sprinkled throughout both ActiveRecord and Devise. Eventually, we realized that we had hit an area in ActiveRecord.find_by where klass !== ProjectManager was true, even though klass was indeed ProjectManager.

Looking for Expert Ruby on Rails Development Services?
See how our experts can help you
SCHEDULE A MEETING

What led us to realize that this was actually an autoloading issue was the fact that klass.object_id was not equal to ProjectManager.object_id. ActiveRecord was holding into a stale version of the ProjectManager class, and the equality comparison for the two classes was not passing because the object ids were different. When ProjectManager had been reloaded in our application, it had not been reloaded in the ActiveRecord gem. Thus, line #240 (specifically, descendants.include?(subclass)) in the code above would return false because the traversal of descendants would produce a different ProjectManager object_id than our codebase.

Fixing the STI Autoloading Issue

Hate to disappoint you if you read this far, but indeed we had to use Monkey Patching to solve this one. Unfortunately, we ultimately had to monkey patch the User class (or any parent class that uses STI and devise) to override the find_sti_class method added by Devise. We were, however, able to limit this monkey patch to development since classes don’t typically reload in production. I don’t love this solution but it enabled our Rails development environment to function similarly to our production environment (this issue didn’t occur in production).

This solution allowed the User and ProjectManager within our application code to match those within Devise, since the method was called from within our application as opposed to within Devise.

Conclusion

I find it very unfortunate that Rails offers a feature that it doesn’t fully support and actively documents issues with. I find Ruby on Rails a great technology, but this seems like poor development practice in my opinion. Hopefully, in a future version, either Single Table Inheritance support is removed, or autoloading actually works alongside it. I was surprised to read the documentation and read that it was essentially known that STI and autoloading don’t play together. The thing is that almost all Rails applications use autoloading in development environments, and this bug is not easy to reason about on its surface level.

Hopefully, this solution helps others. If you are facing an equality issue with a class that utilizes STI, I recommend you start looking at object ids.

Expert Ruby on Rails Development Services

Flatirons Development is a top-rated Ruby on Rails service provider.

Schedule a Meeting

Get the CEO's Take

Handpicked tech insights and trends from our CEO.

E-mail

Expert Ruby on Rails Development Services

Flatirons Development is a top-rated Ruby on Rails service provider.

Schedule a Meeting

Get the CEO's Take

Handpicked tech insights and trends from our CEO.

E-mail
Bryan Villafañe
More ideas.
Digital Product Development
Design

Digital Product Development: Enhance Your Business Offerings

Flatirons

Sep 12, 2025
React SEO
Development

React SEO: Optimize Your React Apps for Search Engines

Flatirons

Sep 07, 2025
Software Engineers Replaced by AI
Development

Will Software Engineers Be Replaced by AI?

Flatirons

Aug 31, 2025
Data Analytics Strategy
Business

Best Data Analytics Strategy for Business Growth

Flatirons

Aug 25, 2025
Ruby Case Statement
Development

Ruby Case Statement: A Simplified Approach to Conditional Logic

Flatirons

Aug 19, 2025
Team Augmentation
Development

Team Augmentation: Enhance Your Workforce Capabilities

Flatirons

Aug 13, 2025
Digital Product Development
Design

Digital Product Development: Enhance Your Business Offerings

Flatirons

Sep 12, 2025
React SEO
Development

React SEO: Optimize Your React Apps for Search Engines

Flatirons

Sep 07, 2025
Software Engineers Replaced by AI
Development

Will Software Engineers Be Replaced by AI?

Flatirons

Aug 31, 2025
Data Analytics Strategy
Business

Best Data Analytics Strategy for Business Growth

Flatirons

Aug 25, 2025
Ruby Case Statement
Development

Ruby Case Statement: A Simplified Approach to Conditional Logic

Flatirons

Aug 19, 2025
Team Augmentation
Development

Team Augmentation: Enhance Your Workforce Capabilities

Flatirons

Aug 13, 2025
Digital Product Development
Design

Digital Product Development: Enhance Your Business Offerings

Flatirons

Sep 12, 2025
React SEO
Development

React SEO: Optimize Your React Apps for Search Engines

Flatirons

Sep 07, 2025
Software Engineers Replaced by AI
Development

Will Software Engineers Be Replaced by AI?

Flatirons

Aug 31, 2025
Data Analytics Strategy
Business

Best Data Analytics Strategy for Business Growth

Flatirons

Aug 25, 2025
Ruby Case Statement
Development

Ruby Case Statement: A Simplified Approach to Conditional Logic

Flatirons

Aug 19, 2025
Team Augmentation
Development

Team Augmentation: Enhance Your Workforce Capabilities

Flatirons

Aug 13, 2025
Digital Product Development
Design

Digital Product Development: Enhance Your Business Offerings

Flatirons

Sep 12, 2025
React SEO
Development

React SEO: Optimize Your React Apps for Search Engines

Flatirons

Sep 07, 2025
Software Engineers Replaced by AI
Development

Will Software Engineers Be Replaced by AI?

Flatirons

Aug 31, 2025
Data Analytics Strategy
Business

Best Data Analytics Strategy for Business Growth

Flatirons

Aug 25, 2025
Ruby Case Statement
Development

Ruby Case Statement: A Simplified Approach to Conditional Logic

Flatirons

Aug 19, 2025
Team Augmentation
Development

Team Augmentation: Enhance Your Workforce Capabilities

Flatirons

Aug 13, 2025