views:

298

answers:

2

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

A: 

Due to the way STI works in Rails (it stores the model name in a database column called 'type') I don't see how it can support the hierarchy you describe above - I think you are limited to a single level hierarchy.

DanSingerman
I do have the Type Column in my Db. At first I thought that I was only limited to one level but at one point an AuthLogic find was looking for all 4 potential types. Now it only looks at the Base
rube_noob
+1  A: 

Is the AuthUser model going to be used by itself?

If it is only a class for shared methods between the inherited classes you could try to set it as an abstract class. That way ActiveRecord might pass right through it.

In the declaration of AuthUser, just add self.abstract_class = true like this:

class AuthUser < User
     self.abstract_class = true
end

I do not know if that works in this scenario, but it could be worth a try.

Jimmy Stenke
The AuthUser class contains the shared functionality of the 3 sub classes. THe purpose of those additional subclasses is to respond differently in terms of permissions
rube_noob
Ok, that should work. I use it like that all the time (but not in the middle of a STI however). In regular cases, by setting it as abstract would tell ActiveRecord to not look for a table for that Model, so in STI it should (according to my logic) not look for that type. It means that if you need to instanciate that model as well it won't work, but only using the subclasses could.
Jimmy Stenke