This project is archived and is in readonly mode.
Model with namespace doesn't work well
Reported by Erico | March 11th, 2011 @ 04:15 PM
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:in
block 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:in
const_missing'
app/controllers/admin/users_controller.rb:5:in index'
actionpack (3.0.4)
lib/action_controller/metal/implicit_render.rb:4:in
send_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:in
process_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.
Comments and changes to this ticket
-
Andrew White March 11th, 2011 @ 08:04 PM
- State changed from new to duplicate
- Importance changed from to Low
This is a duplicate of #5383 - 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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>