简体   繁体   English

Rails:在具有警告的子文件夹中组织模型:顶级由B :: A引用的常量A.

[英]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 ),会发生以下情况。

  • check if User::Info is defined - it is not (yet) 检查是否定义了User::Info - 它还没有(还)
  • check if 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文件并要求它
  • return User::Info return User::Info

Now lets do it again for User::File 现在让我们再次为User::File做一遍

  • check if User::File is defined - it is not (yet) 检查是否定义了User::File - 它还没有(还)
  • check if File is defined - it is (because ruby has a built in File class)! 检查File是否已定义 - 它是(因为ruby有一个内置的File类)!
  • produce a warning, because we've been asked for User::File but got ::File 产生警告,因为我们已经被要求User::File但得到了::File
  • return ::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.

相关问题 使用Rails中的命名空间类防止“警告:用A :: B引用的常量B” - Preventing “warning: toplevel constant B referenced by A::B” with namespaced classes in Rails Rails3:警告:toplevel引用的常量ApplicationController - Rails3: warning: toplevel constant ApplicationController referenced by 警告:toplevel常量引用 - warning: toplevel constant referenced 在子文件夹的滑轨中组织模型/控制器和类 - Organizing models/controllers and classes in rails in subfolders 运行在orm_adapter-0.5.0 / lib / orm_adapter / to_adapter.rb中的Rails + Devise:5:警告:User :: OrmAdapter引用的顶级常量OrmAdapter - Rails + Devise running into orm_adapter-0.5.0/lib/orm_adapter/to_adapter.rb:5: warning: toplevel constant OrmAdapter referenced by User::OrmAdapter Children :: ApplicationController引用的顶级常量ApplicationController - toplevel constant ApplicationController referenced by Children::ApplicationController Rails 3.2.9和子文件夹中的模型 - Rails 3.2.9 and models in subfolders 导轨子文件夹中的模型/控制器? - Models/Controllers in subfolders for rails? Rails模型在子文件夹和关系中 - Rails models in subfolders and relationships Ruby 在 Rails 上。 在子文件夹中组织公共部分 - Ruby on Rails. Organizing common partials in subfolders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM