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.
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:
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.
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.
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.
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.
Flatirons is a top-rated Ruby on Rails service provider.
Handpicked tech insights and trends from our CEO.
Flatirons is a top-rated Ruby on Rails service provider.
Handpicked tech insights and trends from our CEO.
Flatirons
Nov 26, 2024Flatirons
Nov 20, 2024Flatirons
Nov 18, 2024Flatirons
Nov 16, 2024Flatirons
Nov 14, 2024Flatirons
Nov 14, 2024