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:stringclass 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.newNameError: 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:in `const_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> -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Oct-13 08:28 UTC
Re: Single Table Inheritance (STI) Broken: NameError
On Oct 13, 9:21 am, Albert Wong <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> 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/dependen cies.rb:443:in > `load_missing_constant'' > from > /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependen cies.rb:80:in > `const_missing'' > from > /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependen cies.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> > -- > Posted viahttp://www.ruby-forum.com/.
Fred you are the man! That was exactly the problem... Thanks for all the help!... Frederick Cheung 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-- Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- no such file to load -- restClient (MissingSourceFile) HELP
- having some issues with factory_girl and bundler...
- just installed rails on ubuntu, error creating rails dir -->unexpected token: ''#<RubyToken::TkLPAREN:
- Parsing fields with phrases.
- errors while starting console...