2015-07-13 78 views
0

我有「用戶」模型類(應用程序/模型/ user.rb)軌,Model類和模塊衝突

這個類的工作在任何控制器除了在專門的命名空間。

例如,

應用程序/控制器/ 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的(應用程序/模型/ user.rb)

爲什麼這個錯誤出現在那裏是什麼?爲什麼User.class是模塊?
(logger.debug AnyModel.class#=> 「類」)

我真的想知道..

的Rails 4.1.4版本
紅寶石2.2.0p0(情節中字版本49005 )

++編輯(2015年7月14日上午11點53分(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]」 by line 15

但是! 刷新,錯誤行改變成15
錯誤「未初始化不斷Nadmin ::合作伙伴:: ::的AccountController用戶」一行15後(當然,沒有記錄,因爲記錄行是錯誤線)

而且重複刷新,錯誤行保持15.錯誤消息相同。

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

與上面的狀態相同。
(當服務器首先啓動,並通過第16行請求這個動作錯誤「未初始化的常量Nadmin :: Partner :: AccountController :: User」。 和日誌打印「模塊」按行15

但是!刷新後錯誤行更改爲15.
錯誤「未初始化的常量Nadmin :: Partner :: AccountController :: User」,由行15.

並重復刷新一次,錯誤行保持爲15.錯誤消息相同。

回答

0

我明白了!

之所以

有我的地方的工具軟件/控制器/ nadmin /用戶目錄(但是當這個目錄存在,我不知道)(GIT無法跟蹤空文件夾。)

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

並解決它。我可以在nadmin命名空間中使用用戶模型!

感謝這個錯誤,我知道目錄名稱(在控制器文件夾中)可以與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"