Rails、模型 class 和模块冲突
Rails, Model class and Module conflict
我有 'User' 型号 class (app/models/user.rb)
这 class 适用于除特殊命名空间之外的任何控制器。
例如,
app/controllers/chimiseng/user_controller.rb - 用户模型有效!
app/controllers/chimiseng/*_controller.rb - 全部有效!
app/controllers/nadmin/* - 用户模型在任何控制器中都不起作用。
app/controllers/nadmin/合作伙伴/账户_controller.rb
app/controllers/nadmin/日志_controller.rb
..
..
错误信息:
NoMethodError in Nadmin::Partner::AccountController#index
undefined method `where' for Nadmin::User:Module
14: @users = User.where("info_update = true")
然后如果刷新,错误信息改变,
NameError in Nadmin::Partner::AccountController#index
uninitialized constant Nadmin::Partner::AccountController::User
14: @users = User.where("info_update = true")
和
logger.debug User.class # => "Module"
我没有模块用户。
只有 class 个用户 < ActiveRecored::Base (app/models/user.rb)
为什么会出现这个错误?为什么 User.class 是 Module ?
(logger.debugAnyModel.class#=>"Class")
我真的很想知道..
Rails 版本 4.1.4
ruby 2.2.0p0(2014-12-25 修订版 49005)
++编辑(2015-07-14 11:53上午(+09:00))
#nadmin/partner/account_controller.rb#index action
15: logger.debug User.ancestors
16: @user = User.where("info_update = true")
当服务器首先启动时,并请求此操作。第 16 行错误 "uninitialized constant Nadmin::Partner::AccountController::User",第 15 行 log 打印“[Nadmin::User]”
但是! 刷新后,错误行变为15.
error "uninitialized constant Nadmin::Partner::AccountController::User" by line 15.(当然,没有记录,因为行记录是错误行)
然后再次重复刷新,错误行保持15。错误信息相同。
15: logger.debug User.class
16: @user = User.where("info_update = true")
与上述状态相同。
(当服务器首先启动时,并请求此操作。第 16 行错误 "uninitialized constant Nadmin::Partner::AccountController::User"。
和 log 在第 15
行打印 "Module"
但是!刷新后错误行变为15.
第 15 行错误 "uninitialized constant Nadmin::Partner::AccountController::User"。
再重复刷新,错误行保持15。错误信息相同。)
我明白了!!
原因
我本地有一个app/controllers/nadmin/user目录(但我不知道这个目录什么时候存在)(git不跟踪空文件夹..)
所以我 rm -rf app/controllers/nadmin/user.
并解决它。我可以在 nadmin 命名空间中使用用户模型!
由于这个错误,我了解到目录名称(在控制器文件夹中)可能与模型 class 名称冲突。
所以我认为像controller naming convention这样的目录名有利于目录名最后一个单词的复数化。 (或命名目录时注意型号名称)
查看下面我的调试(byebug gem)。
[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"
我有 'User' 型号 class (app/models/user.rb)
这 class 适用于除特殊命名空间之外的任何控制器。
例如,
app/controllers/chimiseng/user_controller.rb - 用户模型有效! app/controllers/chimiseng/*_controller.rb - 全部有效!
app/controllers/nadmin/* - 用户模型在任何控制器中都不起作用。
app/controllers/nadmin/合作伙伴/账户_controller.rb
app/controllers/nadmin/日志_controller.rb
..
..
错误信息:
NoMethodError in Nadmin::Partner::AccountController#index
undefined method `where' for Nadmin::User:Module
14: @users = User.where("info_update = true")
然后如果刷新,错误信息改变,
NameError in Nadmin::Partner::AccountController#index
uninitialized constant Nadmin::Partner::AccountController::User
14: @users = User.where("info_update = true")
和
logger.debug User.class # => "Module"
我没有模块用户。
只有 class 个用户 < ActiveRecored::Base (app/models/user.rb)
为什么会出现这个错误?为什么 User.class 是 Module ?
(logger.debugAnyModel.class#=>"Class")
我真的很想知道..
Rails 版本 4.1.4
ruby 2.2.0p0(2014-12-25 修订版 49005)
++编辑(2015-07-14 11:53上午(+09:00))
#nadmin/partner/account_controller.rb#index action
15: logger.debug User.ancestors
16: @user = User.where("info_update = true")
当服务器首先启动时,并请求此操作。第 16 行错误 "uninitialized constant Nadmin::Partner::AccountController::User",第 15 行 log 打印“[Nadmin::User]”
但是! 刷新后,错误行变为15.
error "uninitialized constant Nadmin::Partner::AccountController::User" by line 15.(当然,没有记录,因为行记录是错误行)
然后再次重复刷新,错误行保持15。错误信息相同。
15: logger.debug User.class
16: @user = User.where("info_update = true")
与上述状态相同。
(当服务器首先启动时,并请求此操作。第 16 行错误 "uninitialized constant Nadmin::Partner::AccountController::User"。
和 log 在第 15
但是!刷新后错误行变为15.
第 15 行错误 "uninitialized constant Nadmin::Partner::AccountController::User"。
再重复刷新,错误行保持15。错误信息相同。)
我明白了!!
原因
我本地有一个app/controllers/nadmin/user目录(但我不知道这个目录什么时候存在)(git不跟踪空文件夹..)
所以我 rm -rf app/controllers/nadmin/user.
并解决它。我可以在 nadmin 命名空间中使用用户模型!
由于这个错误,我了解到目录名称(在控制器文件夹中)可能与模型 class 名称冲突。
所以我认为像controller naming convention这样的目录名有利于目录名最后一个单词的复数化。 (或命名目录时注意型号名称)
查看下面我的调试(byebug gem)。
[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"