问题描述
今天,我决定重组大量与用户相关的模型,但我遇到了问题。
Today I decided to reorganize big amount of user related models and I'm having a problem with it.
在拥有这样的结构之前:
Before I had such structure:
app/models/user.rb
app/models/user_info.rb
app/models/user_file.rb
...
所以我移动了所有 user _
将模型复制到用户子文件夹,如下所示:
So I moved all user_
models to user subfolder like this:
app/models/user.rb
app/models/user/info.rb
app/models/user/file.rb
...
并将其定义更改为
class User::Info < ActiveRecord::Base
class User::File < ActiveRecord::Base
...
User
模型未更改(关联除外)。
User
model wasn't changed (except associations).
除 User :: File
之外,其他所有功能都正常模型。当我尝试访问此模型时,出现以下错误:
Everything works fine except User::File
model. When i'm trying to access this model i get the following error:
warning: toplevel constant File referenced by User::File
并确实返回标准的ruby File类。
and indeed it returns standard ruby File class.
我在做什么错了?
UPD1:
root# rails c
Loading development environment (Rails 3.2.13)
2.0.0p195 :001 > User::File
(irb):1: warning: toplevel constant File referenced by User::File
=> File
2.0.0p195 :002 > User::Info
=> User::Info(...)
UPD2:
2.0.0p195 :001 > User::SomeModel
NameError: uninitialized constant User::SomeModel
2.0.0p195 :002 > User::IO
(irb):2: warning: toplevel constant IO referenced by User::IO
=> IO
2.0.0p195 :003 > User::Kernel
(irb):3: warning: toplevel constant Kernel referenced by User::Kernel
=> Kernel
我的应用程序没有任何IO或内核类,除了ruby默认。
My app doesn't have any IO or Kernel classes, except ruby default.
UPD3:
# app/models/user.rb
class User < ActiveRecord::Base
has_many :files, class_name: 'User::File'
..
end
# app/models/user/file.rb
class User::File < ActiveRecord::Base
belongs_to :user
# some validations, nothing serious
end
推荐答案
更新:今年的圣诞节礼物是Ruby 2.5.0发行版,该错误不再发生。使用Ruby 2.5+,您将得到所需的常数或错误。对于较旧的Ruby版本,请继续阅读:
Update: This years Christmas present was the release of Ruby 2.5.0 with which this error won't happen anymore. With Ruby 2.5+ you will either get the constant you asked for or an error. For older Ruby versions read on:
您的 User :: File
类未加载。您必须要求它(例如在 user.rb
中)。
Your User::File
class is not loaded. You have to require it (e.g. in user.rb
).
当ruby / rails看到 User :: Info
并对其进行评估(简化;仅定义了 User
)。
The following happens when ruby/rails sees User::Info
and evaluates it (simplified; only User
is defined yet).
- 检查是否定义了
User :: Info
-尚未(尚未) - 检查是否定义了
Info
-(尚未) -
未初始化的常量
->用魔力找到user / info.rb
文件并要求它 - 返回
User :: Info
- 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
现在让 User再做一次: :File
- 检查是否
User :: File
已定义-还没有( - 检查是否已定义
File
-是因为(因为ruby具有内置的File
类)! - 发出警告,因为我们被要求输入
User :: File
但得到了:: File
- 返回
:: 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
我们观察到自动需要(尚未)未知常数的文件的rails魔术不适用于 User :: File
,因为文件
是未知的。
We observe that the rails magic, that automatically requires files for (yet) unknown constants, does not work for User::File
because File
is not unknown.
这篇关于Rails:在具有警告的子文件夹中组织模型:B :: A引用的顶级常量A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!