[英]Rails: organizing models in subfolders having warning: toplevel constant A referenced by B::A
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
...
So I moved all user_
models to user subfolder like this: 所以我将所有user_
模型移动到用户子文件夹,如下所示:
app/models/user.rb
app/models/user/info.rb
app/models/user/file.rb
...
and changed their definitions to 并将其定义更改为
class User::Info < ActiveRecord::Base
class User::File < ActiveRecord::Base
...
User
model wasn't changed (except associations). User
模型未更改(关联除外)。
Everything works fine except User::File
model. 除User::File
模型外,一切正常。 When i'm trying to access this model i get the following error: 当我试图访问此模型时,我收到以下错误:
warning: toplevel constant File referenced by User::File
and indeed it returns standard ruby File class. 实际上它返回标准的ruby File类。
What i'm doing wrong? 我做错了什么?
UPD1: 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: 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
My app doesn't have any IO or Kernel classes, except ruby default. 我的应用程序没有任何IO或内核类,除了ruby默认值。
UPD3: 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
Update : This years Christmas present was the release of Ruby 2.5.0 with which this error won't happen anymore. 更新 :今年圣诞礼物是Ruby 2.5.0的发布,不再发生此错误。 With Ruby 2.5+ you will either get the constant you asked for or an error. 使用Ruby 2.5+,您将获得所要求的常量或错误。 For older Ruby versions read on: 对于较旧的Ruby版本,请阅读:
Your User::File
class is not loaded. 您的User::File
类未加载。 You have to require it (eg in user.rb
). 您必须要求它(例如在user.rb
)。
The following happens when ruby/rails sees User::Info
and evaluates it (simplified; only User
is defined yet). 当ruby / rails看到User::Info
并对其进行评估时(简化;仅定义User
),会发生以下情况。
User::Info
is defined - it is not (yet) 检查是否定义了User::Info
- 它还没有(还) Info
is defined - it is not (yet) 检查Info
是否已定义 - 它尚未(尚未) uninitialized constant
-> do rails magic to find the user/info.rb
file and require it uninitialized constant
- >执行rails magic以查找user/info.rb
文件并要求它 User::Info
return User::Info
Now lets do it again for User::File
现在让我们再次为User::File
做一遍
User::File
is defined - it is not (yet) 检查是否定义了User::File
- 它还没有(还) File
is defined - it is (because ruby has a built in File
class)! 检查File
是否已定义 - 它是(因为ruby有一个内置的File
类)! User::File
but got ::File
产生警告,因为我们已经被要求User::File
但得到了::File
::File
return ::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魔法,它自动需要(尚未)常量的文件,对User::File
不起作用,因为File
不是未知的。
Try referring to the class as User::File to distinguish it from regular ruby Files. 尝试将类称为User :: File,以区别于常规ruby文件。 You can use ::File to refer to those when it is ambiguous 你可以使用:: File来指代它不明确的那些
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.