Forum: Ruby on Rails STI and subclassing twice

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Efb470329d5342e1b54a9338ceb330c6?d=identicon&s=25 José Ignacio (ji123)
on 2009-05-08 12:05
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?
Efb470329d5342e1b54a9338ceb330c6?d=identicon&s=25 José Ignacio (ji123)
on 2009-05-11 09:52
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-11 10:38
(Received via mailing list)
On May 11, 8:52 am, José Ignacio <rails-mailing-l...@andreas-s.net>
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
Efb470329d5342e1b54a9338ceb330c6?d=identicon&s=25 José Ignacio (ji123)
on 2009-05-11 12:08
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'
This topic is locked and can not be replied to.