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 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 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.
Essentials of Digital Product Design
Business

Learn the Essentials of Digital Product Design

Flatirons

Nov 26, 2024
Ruby on Rails Alternatives
Development

10 Ruby on Rails Alternatives You Should Know About

Flatirons

Nov 20, 2024
Practice EHR Integration
Development

Streamline Your Healthcare Practice with EHR Integration

Flatirons

Nov 18, 2024
React Developer Salary
Development

Unlock Your Potential: React Developer Salary Guide

Flatirons

Nov 16, 2024
rails vs python
Development

Ruby on Rails vs Python: Key Differences

Flatirons

Nov 14, 2024
BCM
Development

Understanding Your Car’s BCM – Key Auto Insights

Flatirons

Nov 14, 2024
Essentials of Digital Product Design
Business

Learn the Essentials of Digital Product Design

Flatirons

Nov 26, 2024
Ruby on Rails Alternatives
Development

10 Ruby on Rails Alternatives You Should Know About

Flatirons

Nov 20, 2024
Practice EHR Integration
Development

Streamline Your Healthcare Practice with EHR Integration

Flatirons

Nov 18, 2024
React Developer Salary
Development

Unlock Your Potential: React Developer Salary Guide

Flatirons

Nov 16, 2024
rails vs python
Development

Ruby on Rails vs Python: Key Differences

Flatirons

Nov 14, 2024
BCM
Development

Understanding Your Car’s BCM – Key Auto Insights

Flatirons

Nov 14, 2024
Essentials of Digital Product Design
Business

Learn the Essentials of Digital Product Design

Flatirons

Nov 26, 2024
Ruby on Rails Alternatives
Development

10 Ruby on Rails Alternatives You Should Know About

Flatirons

Nov 20, 2024
Practice EHR Integration
Development

Streamline Your Healthcare Practice with EHR Integration

Flatirons

Nov 18, 2024
React Developer Salary
Development

Unlock Your Potential: React Developer Salary Guide

Flatirons

Nov 16, 2024
rails vs python
Development

Ruby on Rails vs Python: Key Differences

Flatirons

Nov 14, 2024
BCM
Development

Understanding Your Car’s BCM – Key Auto Insights

Flatirons

Nov 14, 2024
Essentials of Digital Product Design
Business

Learn the Essentials of Digital Product Design

Flatirons

Nov 26, 2024
Ruby on Rails Alternatives
Development

10 Ruby on Rails Alternatives You Should Know About

Flatirons

Nov 20, 2024
Practice EHR Integration
Development

Streamline Your Healthcare Practice with EHR Integration

Flatirons

Nov 18, 2024
React Developer Salary
Development

Unlock Your Potential: React Developer Salary Guide

Flatirons

Nov 16, 2024
rails vs python
Development

Ruby on Rails vs Python: Key Differences

Flatirons

Nov 14, 2024
BCM
Development

Understanding Your Car’s BCM – Key Auto Insights

Flatirons

Nov 14, 2024