STI and subclassing twice

I’m trying using STI to implement a hiearchy similar to this:

script/generate scaffold name:string type:string

class Person < ActiveRecord::Base; end
class Customer < Person; end
class Employee < Person; end
class Developer < Employee; end

I.e., there are two levels of inheritance (Developer < Employee <
Person).

When creating a Customer, Rails understands it’s a Person, but if I
create a Developer, Rails ‘tends to forget’ it’s an Employee. In other
words:

$ script/console

Developer.create :name=>‘Bob’
=> #<Developer id: 1, name: “Bob”, type: “Developer”, created_at:
“2009-05-08 09:59:58”, updated_at: “2009-05-08 09:59:58”>

exit

$ script/console

Employee.find :all
=> []

Person.find :all
=> [#<Developer id: 1, name: “Bob”, type: “Developer”, created_at:
“2009-05-08 09:59:58”, updated_at: “2009-05-08 09:59:58”>]

Worst of all, if I change the order of the ‘find’ statements, things
work:

$ script/console

Person.find :all
=> [#<Developer id: 1, name: “Bob”, type: “Developer”, created_at:
“2009-05-08 09:59:58”, updated_at: “2009-05-08 09:59:58”>]

Employee.find :all
=> [#<Developer id: 1, name: “Bob”, type: “Developer”, created_at:
“2009-05-08 09:59:58”, updated_at: “2009-05-08 09:59:58”>]

Maybe this has to do with some caching stuff… any ideas?

Any idea? I think there might be a bug here. I’m using Rails 2.3.2, but
I get the same behaviour in Rails 2.2.2.

On May 11, 8:52 am, José Ignacio [email protected]
wrote:

Any idea? I think there might be a bug here. I’m using Rails 2.3.2, but
I get the same behaviour in Rails 2.2.2.

There’s some funky stuff with STI that basically boils down to the
fact that if a class hasn’t been loaded yet rails doesn’t know it
exists (and so doesn’t create the correct query).
One way of doing things is to force these classes to be loaded, eg at
the bottom of person.rb put

require_dependency ‘customer’
require_dependency ‘employee’
require_dependency ‘developer’

or something like that.

Fred

Interesting… That makes everything work as expected! Thanks

There’s some funky stuff with STI that basically boils down to the
fact that if a class hasn’t been loaded yet rails doesn’t know it
exists (and so doesn’t create the correct query).
One way of doing things is to force these classes to be loaded, eg at
the bottom of person.rb put

require_dependency ‘customer’
require_dependency ‘employee’
require_dependency ‘developer’