Bas van Westing
2007-Nov-18 14:00 UTC
environment variables unknown in Model with Namespace
Hi, I have a problem with a model which is defined within a namespace. It does not seem to find another namespace that was defined in the environment. #config/initializers.rb module X class Exception < Exception ...some custom exception definitions here... end end #app/models/y/my_model.rb class Y::MyModel < ActiveRecord::Base validates_presence_of :attr, :message => X::Exception.new(''init).to_string_reference end This setup fails during boot because the model searches for Y::MyModel::X, which it cannot find: "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ dependencies.rb:478:in `const_missing'':NameError: uninitialized constant Y::MyModel::X" When I try the following setup: #app/models/y/my_model.rb class Y::MyModel < ActiveRecord::Base validates_presence_of :attr, :message => ::X::Exception.new(''init).to_string_reference end It still fails during boot because it still cannot find X: "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ dependencies.rb:266:in `load_missing_constant'':NameError: uninitialized constant X" However, in my normal models (without a namespace) it does work: #app/models/my_other_model.rb class MyOtherModel < ActiveRecord::Base validates_presence_of :attr, :message => X::Exception.new(''init).to_string_reference end This loads fine. I don''t understand why it does not work in the namespace situation. Does anybody know what I''m doing wrong? I''m using Rails 2.0RC1 by the way. Thanks in advance, Bas van Westing --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Nov-18 15:25 UTC
Re: environment variables unknown in Model with Namespace
On Nov 18, 2007, at 3:00 PM, Bas van Westing wrote:> I have a problem with a model which is defined within a namespace. It > does not seem to find another namespace that was defined in the > environment. > > #config/initializers.rb > module X > class Exception < Exception > ...some custom exception definitions here... > end > end > > #app/models/y/my_model.rb > class Y::MyModel < ActiveRecord::Base > validates_presence_of :attr, :message => > X::Exception.new(''init).to_string_reference > end > > This setup fails during boot because the model searches for > Y::MyModel::X, which it cannot find: > "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ > dependencies.rb:478:in `const_missing'':NameError: uninitialized > constant Y::MyModel::X" > > When I try the following setup: > > #app/models/y/my_model.rb > class Y::MyModel < ActiveRecord::Base > validates_presence_of :attr, :message > => ::X::Exception.new(''init).to_string_reference > end > > It still fails during boot because it still cannot find X: > "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ > dependencies.rb:266:in `load_missing_constant'':NameError: > uninitialized constant X" > > However, in my normal models (without a namespace) it does work: > > #app/models/my_other_model.rb > class MyOtherModel < ActiveRecord::Base > validates_presence_of :attr, :message => > X::Exception.new(''init).to_string_reference > end > > This loads fine. I don''t understand why it does not work in the > namespace situation. Does anybody know what I''m doing wrong? I''m using > Rails 2.0RC1 by the way.To give a brief explanation constant autoloading assumes two things about a missing constant with qualified name "Foo::Bar": 1. The definition lives in a file whose filename ends with "Foo::Bar".underscore + ''.rb'' 2. The resulting path "foo/bar.rb" exists under one of the directories in Dependencies.load_path When Rails boots Dependencies.load_path is initialized with an array of standard directories (like RAILS_ROOT/app/models and friends), but since "X" is defined in config/initializers.rb it won''t be autoloaded (that file is non-standard AFAIK, are you sure that''s its name?). -- fxn --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bas van Westing
2007-Nov-20 20:41 UTC
Re: environment variables unknown in Model with Namespace
Hi Xavier, The config/initializer.rb was a typo. It is in config/initializers/ error_codes.rb The config/initializers/* files are loaded on boot by default with rails 2.0RC1. The problem is still unclear to me because the constant is available in the other model (without the module/namespace). Both models are loaded in the same boot, so that cannot be the problem. Do you have any more ideas? Thanks, Bas On Nov 18, 4:25 pm, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Nov 18, 2007, at 3:00 PM,BasvanWestingwrote: > > > > > I have a problem with a model which is defined within a namespace. It > > does not seem to find another namespace that was defined in the > > environment. > > > #config/initializers.rb > > module X > > class Exception < Exception > > ...some custom exception definitions here... > > end > > end > > > #app/models/y/my_model.rb > > class Y::MyModel < ActiveRecord::Base > > validates_presence_of :attr, :message => > > X::Exception.new(''init).to_string_reference > > end > > > This setup fails during boot because the model searches for > > Y::MyModel::X, which it cannot find: > > "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ > > dependencies.rb:478:in `const_missing'':NameError: uninitialized > > constant Y::MyModel::X" > > > When I try the following setup: > > > #app/models/y/my_model.rb > > class Y::MyModel < ActiveRecord::Base > > validates_presence_of :attr, :message > > => ::X::Exception.new(''init).to_string_reference > > end > > > It still fails during boot because it still cannot find X: > > "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ > > dependencies.rb:266:in `load_missing_constant'':NameError: > > uninitialized constant X" > > > However, in my normal models (without a namespace) it does work: > > > #app/models/my_other_model.rb > > class MyOtherModel < ActiveRecord::Base > > validates_presence_of :attr, :message => > > X::Exception.new(''init).to_string_reference > > end > > > This loads fine. I don''t understand why it does not work in the > > namespace situation. Does anybody know what I''m doing wrong? I''m using > > Rails 2.0RC1 by the way. > > To give a brief explanation constant autoloading assumes two things > about a missing constant with qualified name "Foo::Bar": > > 1. The definition lives in a file whose filename ends > with "Foo::Bar".underscore + ''.rb'' > > 2. The resulting path "foo/bar.rb" exists under one of the > directories in Dependencies.load_path > > When Rails boots Dependencies.load_path is initialized with an array > of standard directories (like RAILS_ROOT/app/models and friends), but > since "X" is defined in config/initializers.rb it won''t be autoloaded > (that file is non-standard AFAIK, are you sure that''s its name?). > > -- fxn--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---