今天遇到了這個問題,在網上搜一下找到一個滿詳盡的解釋,茅塞頓開:
YourUser::Fileclass is not loaded. You have to require it (e.g. inuser.rb).The following happens when ruby/rails seesUser::Infoand evaluates it (simplified; onlyUseris defined yet).
- check if
User::Infois defined - it is not (yet)- check if
Infois defined - it is not (yet)uninitialized constant-> do rails magic to find theuser/info.rbfile and require it- return
User::InfoNow lets do it again forUser::File
- check if
User::Fileis defined - it is not (yet)- check if
Fileis defined - it is (because ruby has a built inFileclass)!- produce a warning, because we've been asked for
User::Filebut got::File- return
::File
裡面提到了要手動 Require NameSpace 下的 Model,但是如果 NS 下的 model 本身有繼承到 NS 外的 Model,要確保在父類別宣告後建立才行。所以最好是在父類別檔案的結尾處 require。