今天我决定重新组织大量与用户相关的模型,但我遇到了问题。
在我有这样的结构之前:
app/models/user.rb
app/models/user_info.rb
app/models/user_file.rb
...
所以我将所有user_
模型移动到用户子文件夹,如下所示:
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::File
除模型外,一切正常。当我尝试访问此模型时,出现以下错误:
warning: toplevel constant File referenced by User::File
实际上它返回标准的 ruby File 类。
我做错了什么?
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 默认值。
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