【问题标题】:Rails, Model class and Module conflictRails,模型类和模块冲突
【发布时间】:2015-07-13 08:11:53
【问题描述】:

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

这个类可以在除特殊命名空间之外的任何控制器上工作。

例如,

app/controllers/chimiseng/user_controller.rb - 用户模型有效! app/controllers/chimiseng/*_controller.rb - 所有功能!

app/controllers/nadmin/* - 用户模型在任何控制器中都不起作用。 应用程序/控制器/nadmin/partner/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"

我没有模块用户。

只有类 User

为什么会出现这个错误?为什么 User.class 是 Module ?
(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")

当服务器首先启动时,并请求此操作。第 16 行出现错误“未初始化的常量 Nadmin::Partner::AccountController::User”。第 15 行出现 log print "[Nadmin::User]"

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

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

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

与上面的状态相同。
(当服务器首先启动并请求此操作时。第 16 行出现错误“未初始化的常量 Nadmin::Partner::AccountController::User”。 并在第 15 行 log print "Module"

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

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

【问题讨论】:

    标签: ruby-on-rails rails-activerecord


    【解决方案1】:

    我明白了!!

    原因

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

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

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

    由于这个错误,我知道目录名称(在控制器文件夹中)可能与模型类名称冲突。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多