Rails, Model class and Module conflict
我有'User'模型类(app / models / user.rb)
该类可在除特殊命名空间外的任何控制器上工作。
例如
app / controllers / chimiseng / user_controller.rb-用户模型有效!
app / controllers / chimiseng / * _ controller.rb-全部可用!
app / controllers / nadmin / *-用户模型不适用于任何控制器。
app / controllers / nadmin / partner / account_controller.rb
app / controllers / nadmin / log_controller.rb
..
..
错误消息:
1 2 3 4 5 | NoMethodError in Nadmin::Partner::AccountController#index undefined method `where' for Nadmin::User:Module 14: @users = User.where("info_update = true") |
,然后如果刷新,则错误消息更改,
1 2 3 4 | NameError in Nadmin::Partner::AccountController#index uninitialized constant Nadmin::Partner::AccountController::User 14: @users = User.where("info_update = true") |
和
1 | logger.debug User.class # =>"Module" |
我没有模块用户。
只有类User 为什么会出现此错误?为什么User.class是Module? 我真的很想知道.. Rails版本4.1.4 编辑(2015-07-14 11:53 am(09:00)) 首先启动服务器,然后请求此操作。第16行出现错误"未初始化的常量Nadmin :: Partner :: AccountController :: User",并在第15行记录了日志打印" [[Nadmin :: User]"" 但是!刷新后,错误行更改为15。 然后再次重复刷新,错误行保持15。错误消息是相同的。 与上面的状态相同。 但是!刷新后,错误行更改为15。
(logger.debug AnyModel.class#=>" Class")
ruby2.2.0p0(2014-12-25修订版49005)
2
3
4
15: logger.debug User.ancestors
16: @user = User.where("info_update = true")
第15行出现错误"未初始化的常量Nadmin :: Partner :: AccountController :: User"。(当然,没有记录,因为行记录是错误行)
2
16: @user = User.where("info_update = true")
(当服务器首先启动并请求此操作时。第16行出现错误"未初始化的常量Nadmin :: Partner :: AccountController :: User"。
并通过第15行记录打印"模块"
第15行出现错误"未初始化的常量Nadmin :: Partner :: AccountController :: User"。
然后再次重复刷新,错误行保持15。错误消息相同。)
我明白了!
原因
我的本地目录中有一个app / controllers / nadmin / user目录(但我不知道该目录何时存在)(git无法跟踪空文件夹。)
所以我rm -rf app / controllers / nadmin / user。
并解决它。我可以在nadmin名称空间中使用用户模型!
由于这个错误,我知道目录名称(在controllers文件夹中)可能与Model类名称冲突。
因此,我认为目录名称(如控制器命名约定)有利于目录名称中最后一个单词的复数形式。 (或在命名目录时关注型号名称)
请参阅下面的调试(byebug gem)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [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" |