Search code examples
ruby-on-railsactiverecordsingle-table-inheritance

STI Inheiritance in Rails. Issues with find


I am having an issue with searching for records in my STI table due to my inheritance structure

class User < ActiveRecord::Base

class LegacyUser < User

class AuthUser < User

class SuperUser < AuthUser

class FieldUser < AuthUser

class ClientAdmin < AuthUser

The problem is that find does not work for the AuthUser Model. The query is looking for type "AuthUser" and does not include the other three possibilities.

Edit: While playing around with this it started to work but only for ClientAdmin and FieldUser so it seems this functionality should be build in. but now it has gone back to the original issue


Solution

  • I just ran into this same issue and found a solution here.

    In short, the issue is that there is no way for your intermediate abstract class to know what its subclasses are before they are loaded. To work around this, you can manually load all the subclasses at the bottom of that classes' file.

    So at the bottom of auth_user.rb:

    require_dependency 'super_user'
    require_dependency 'field_user'
    require_dependency 'client_user'