Rails,模型类和模块冲突

时间:2015-07-13 08:11:53

标签: ruby-on-rails rails-activerecord

我有'用户'模型类(app / models / user.rb)

此类适用于除特殊名称空间外的任何控制器。

例如,

app / controllers / chimiseng / user_controller.rb - 用户模型有效! app / controllers / chimiseng / * _ controller.rb - 全部工作!

app / controllers / nadmin / * - 用户模型无效在任何控制器中。 应用程序/控制器/ nadmin /合作伙伴/ account_controller.rb
应用程序/控制器/ nadmin / log_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"

我没有模块用户。

只有类用户< ActiveRecored :: Base(app / models / user.rb)

为什么会出现此错误?为什么User.class是模块?
(logger.debug AnyModel.class#=>“Class”)

我真的很想知道..

Rails版本4.1.4
ruby 2.2.0p0(2014-12-25修订版49005)

++编辑(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")

当服务器首先启动时,请求此操作。错误“未初始化的常量Nadmin :: Partner :: AccountController :: User”第16行和日志打印“[Nadmin :: User]”第15行

但是! 刷新后,错误行变为15 错误“未初始化的常量Nadmin :: Partner :: AccountController :: User”第15行。(当然,没有记录,因为行记录是错误行)

再次重复刷新,错误行保持15.错误信息相同。

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

与上述状态相同。
(当服务器首先启动时,请求此操作。错误“未初始化的常量Nadmin :: Partner :: AccountController :: User”第16行。     和日志打印“模块”第15行

但是!刷新后,错误行变为15 错误“未初始化的常量Nadmin :: Partner :: AccountController :: User”第15行。

再次重复刷新,错误行保持15.错误信息相同。)

1 个答案:

答案 0 :(得分:0)

我明白了!

原因

我的本​​地有一个app / controllers / nadmin / user目录(但我不知道这个目录何时存在)(git不跟踪空文件夹..)

所以我rm -rf app / controllers / nadmin / user。

并解决它。我可以在nadmin命名空间中使用User模型!

由于这个错误,我知道目录名(在controllers文件夹中)可能与Model类名冲突。

所以我认为像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"