On 12 May 2010 13:50, Mislav Marohnić <
mislav....@gmail.com> wrote:
> Isn't the problem obvious? ActiveRecord cannot list all subclasses under STI
> until it loads all modes in the app. In development mode it relies on
> autoloading and never preloads all models.
> The local solution: require statements in the end of "user.rb":
> require 'manager'
> require 'agent'
> So when User is preloaded, all subclasses will get loaded and be known to
> ActiveRecord too.
> The global solution: ActiveRecord shouldn't rely on obtaining a list of all
> subclasses. Instead, when you query on a parent model, it shouldn't specify
> any classes at all. Thus:
> User.all
> # SELECT * FROM users
> However, this is not adequate with complex STI schemes where there are
> grandchildren. If we're querying on a child that has subclasses, we still
> have to know what they are:
> Manager.all
> # SELECT * from users WHERE users.type = 'Manager'
> # OR users.type = 'EvilManager' OR users.type = 'GoodManager'
Can you explain why when the OP does
Agent.find_by_name 'agent'
it generates
SELECT "users".* FROM "users" WHERE (("users"."type" = 'Agent' OR
"users"."type" = 'SuperAgent')) AND ("users"."name" = 'agent') LIMIT 1
I would have expected only type Agent to be allowed, it appears to be
returning SuperAgents also. If the user of name 'agent' is actually a
SuperAgent I would not expect Agent.find_by_name to find it. Note
that both Agent and SuperAgent are directly derived from User. Unless
the OP is leading us astray.
Colin