Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Model with namespace doesn't work well

#6563

Hi guys,

I create a controller and a model into a namespace but a very strange thing happens.

Here is my code:

class Admin::UsersController < ApplicationController
def index

    records1 = User.all
    records2 = User.all

end end

The first code go normal, but the second occurs this errors:

ArgumentError in Admin/usersController#index
Admin is not missing constant User!

activesupport (3.0.4) lib/active_support/dependencies.rb:479:in load_missing_constant' activesupport (3.0.4) lib/active_support/dependencies.rb:183:inblock in const_missing'
activesupport (3.0.4) lib/active_support/dependencies.rb:181:in each' activesupport (3.0.4) lib/active_support/dependencies.rb:181:inconst_missing'
app/controllers/admin/users_controller.rb:5:in index' actionpack (3.0.4) lib/action_controller/metal/implicit_render.rb:4:insend_action'
actionpack (3.0.4) lib/abstract_controller/base.rb:150:in process_action' actionpack (3.0.4) lib/action_controller/metal/rendering.rb:11:inprocess_action'
...

But when I use records2 = Admin::User.all, everything goes normal, very strange, I believe the application must have to assume the controller is in the same space and get the model from the namespace "Admin", even works, but just once, the second command give the error above, very strange.

Reported by Erico · March 11th, 2011 @ 04:15 PM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • State changed from new to duplicate
    • Importance changed from to Low

    This is a duplicate of #5383 AS dependencies resolver broken: not missing constant error - if you rewrite your class definitions so that they're not inline everything will work, e.g:

    module Admin
      class UsersController < ApplicationController
        @users = User.all
      end
    end
    

    This is because Ruby searches for constants in Object first when defined as ModuleName::ClassName but inside the parent module when nested properly. The first call works because the constant hasn't been loaded yet but the second call finds the already loaded constant. It works fine either way in production because all the models are preloaded. I had a look at seeing if it is fixable but there's no way of intercepting the lookup.

    March 11th, 2011 @ 08:04 PM