Jeff Winterpants
2005-Nov-25 04:09 UTC
Models and controllers organized under module with same name possible?
Howdy, I would like to group related controllers within a module, and have related models under a module of the same name. I start off with the model: micand@coconut ~/dev/projects/tmp $ script/generate model Container::Model create app/models/container create test/unit/container create test/fixtures/container create app/models/container/model.rb create test/unit/container/model_test.rb create test/fixtures/container/models.yml Seems easy enough, but all does not work as it should. micand@coconut ~/dev/projects/tmp $ script/console Loading development environment.>> Container::ModelNameError: uninitialized constant Container from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:200:in `const_missing'' from (irb):1 To fix this, I create app/models/container.rb with the following contents: module Container end require_dependency File.join(File.dirname(__FILE__), ''container'', ''model'') Sweet success! micand@coconut ~/dev/projects/tmp $ script/console Loading development environment.>> Container::Model=> Container::Model The problem comes after I add the controller. Creation works fine: micand@coconut ~/dev/projects/tmp $ script/generate controller Container:: Controller create app/controllers/container create app/helpers/container create app/views/container/controller create test/functional/container create app/controllers/container/controller_controller.rb create test/functional/container/controller_controller_test.rb create app/helpers/container/controller_helper.rb But it is no longer possible to refer to the model class. micand@coconut ~/dev/projects/tmp $ script/console Loading development environment.>> Container::ModelNameError: uninitialized constant Model from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:195:in `const_missing'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:72:in `const_missing'' from (irb):2 It seems having a controller module with the same name as a model module somehow defeats Rails'' automatic require()''ing of needed model files. Adding "require File.join(RAILS_ROOT, ''app'', ''models'', ''container'')" to environment.rb doesn''t resolve this issue, as the forced inclusion of the model module forces out the controller module: micand@coconut ~/dev/projects/tmp $ script/console Loading development environment.>> Container::Model=> Container::Model>> Container::ControllerControllerNameError: uninitialized constant ControllerController from /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:200:in `const_missing'' from (irb):2 Is there any way to have models and controllers co-exist within a module of a particular name, or is the only option to give different names to the module housing the controllers and the module housing the models? Thanks, Jeff __________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com