Single Table Inheritance (STI) Broken: NameError

Some how I’m getting NameError: uninitialized constant when accessing a
child models that inherits form the parent model. However, if I access
the parent model first, then the child model is resolved.

What am I doing wrong? My steps are below…

Thanks,
Francis.

./script/generate model Blah type:string
class CreateBlahs < ActiveRecord::Migration
def self.up
create_table :blahs do |t|
t.string :type

  t.timestamps
end

end

def self.down
drop_table :blahs
end
end

rake db:migrate
== CreateBlahs: migrating
====================================================
– create_table(:blahs)
-> 0.0989s
== CreateBlahs: migrated (0.0991s)
===========================================

blah.rb:
class Blah < ActiveRecord::Base
end

class Whale < Blah
end

class Dolphin < Blah
end

./script/console

Dolphin.new
NameError: uninitialized constant Dolphin
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in
load_missing_constant' from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:inconst_missing’
from
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in
`const_missing’
from (irb):1

Blah.new
=> #<Blah id: nil, type: nil, created_at: nil, updated_at: nil>

Dolphin.new
=> #<Dolphin id: nil, type: “Dolphin”, created_at: nil, updated_at: nil>

On Oct 13, 9:21 am, Albert W. [email protected]
wrote:

Some how I’m getting NameError: uninitialized constant when accessing a
child models that inherits form the parent model. However, if I access
the parent model first, then the child model is resolved.

What am I doing wrong? My steps are below…

When the constant Dolphin is hit for the first time and is not already
loaded, rails expects to find it in dolphin.rb, which doesn’t exist
(and at this point rails can’t know that Dolphin and Blah are
related). If you touch Blah then blah.rb is loaded which will also
cause Dolphin to be loaded. Either stick to the file naming
conventions that allow rails to guess where to load classes from or
don’t rely on it working.

Fred

Fred you are the man! That was exactly the problem… Thanks for all
the help!..

Frederick C. wrote:

When the constant Dolphin is hit for the first time and is not already
loaded, rails expects to find it in dolphin.rb, which doesn’t exist
(and at this point rails can’t know that Dolphin and Blah are
related). If you touch Blah then blah.rb is loaded which will also
cause Dolphin to be loaded. Either stick to the file naming
conventions that allow rails to guess where to load classes from or
don’t rely on it working.

Fred