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