0
votes

I have 'User' model class (app/models/user.rb)

This class works at any controller except in specially namespace.

for example,

app/controllers/chimiseng/user_controller.rb - User model works! app/controllers/chimiseng/*_controller.rb - works all!

app/controllers/nadmin/* - User model not works in any controller. app/controllers/nadmin/partner/account_controller.rb
app/controllers/nadmin/log_controller.rb .. ..

error message :

NoMethodError in Nadmin::Partner::AccountController#index

undefined method `where' for Nadmin::User:Module

14: @users = User.where("info_update = true")

and then if refresh, error message change,

NameError in Nadmin::Partner::AccountController#index
uninitialized constant Nadmin::Partner::AccountController::User

14: @users = User.where("info_update = true")

and

logger.debug User.class # => "Module"

I have no module User.

There is only class User < ActiveRecored::Base (app/models/user.rb)

Why this error appear ? Why User.class is Module ?
(logger.debug AnyModel.class # => "Class")

I really want to know..

Rails version 4.1.4
ruby 2.2.0p0 (2014-12-25 revision 49005)

++Edited (2015-07-14 11:53 am (+09:00))

#nadmin/partner/account_controller.rb#index action

15: logger.debug User.ancestors
16: @user = User.where("info_update = true")

when server start first, and request this action. error "uninitialized constant Nadmin::Partner::AccountController::User" by line 16. and log print "[Nadmin::User]" by line 15

But! after refresh, error line change into 15.
error "uninitialized constant Nadmin::Partner::AccountController::User" by line 15. (of course, no logging because line logging is error line)

And Repeat refresh again, error line keep 15. error message is same.

15: logger.debug User.class
16: @user = User.where("info_update = true")

is same status above.
(when server start first, and request this action. error "uninitialized constant Nadmin::Partner::AccountController::User" by line 16. and log print "Module" by line 15

But! after refresh, error line change into 15.
error "uninitialized constant Nadmin::Partner::AccountController::User" by line 15.

And Repeat refresh again, error line keep 15. error message is same.)

1

1 Answers

0
votes

I got it !!

The reason why

there is a app/controllers/nadmin/user directory in my local (but i don't know when this directory exists) (git not track empty folder..)

so I rm -rf app/controllers/nadmin/user.

and Solve it. I can use User model in nadmin namespace!

Thanks to this error, I get to know directory name (in controllers folder) can conflict with Model class name.

So I think directory name like controller naming convention favors pluralization of the last word in the directory name. (or concern model name when naming directory)

See my debugging (byebug gem) below.

[416, 425] in /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb
416:     # matching the expected path suffix. If found, the module is created and
417:     # assigned to +into+'s constants with the name +const_name+. Provided that
418:     # the directory was loaded from a reloadable base path, it is added to the
419:     # set of constants that are to be unloaded.
420:     def autoload_module!(into, const_name, qualified_name, path_suffix)
=> 421:    return nil unless base_path = autoloadable_module?(path_suffix)
422:       mod = Module.new
423:       into.const_set const_name, mod
424:       autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
425:       mod
(byebug) base_path
nil
(byebug) path_suffix
"nadmin/user"
(byebug) n

[417, 426] in /Users/KimJaeseong/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb
417:     # assigned to +into+'s constants with the name +const_name+. Provided that
418:     # the directory was loaded from a reloadable base path, it is added to the
419:     # set of constants that are to be unloaded.
420:     def autoload_module!(into, const_name, qualified_name, path_suffix)
421:       return nil unless base_path = autoloadable_module?(path_suffix)
=> 422:       mod = Module.new
423:       into.const_set const_name, mod
424:       autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
425:       mod
426:     end
(byebug) base_path
"/Users/KimJaeseong/rails_project/chimiseng/app/controllers"
(byebug) path_suffix
"nadmin/user"