【发布时间】:2013-01-03 16:48:38
【问题描述】:
在app/services,我有一些课程,如Notification::Finder 和Notification::Builder。
它们被放置为app/services/notification/builder.rb 和app/services/notification/finder.rb。
还有Notification 类作为模型,在app/models/notification.rb
autoload_path 配置为config.autoload_paths += %W(#{config.root}/app/services)
当我尝试加载 Finder 时,它可以工作:
Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Finder
=> Notification::Finder
但是当我尝试Builder 时,我遇到了rails 自动加载的问题:
Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Builder
=> ActiveRecord::Associations::Builder
当常量名称(Builder)已经被其他命名空间定义时,它只是忽略我使用的命名空间,并获取ActiveRecord::Associations::Builder。
这是预期的行为,还是 Rails 错误?
更详细地说,activesupport/dependencies.rb 处的 const_missing 方法接收到 const_name 'Builder' 和 nesting.inspect => 'nil'。
很好奇,当我使用 constantize 时,它会按预期解决:
Loading development environment (Rails 3.2.9)
[1] pry(main)> 'Notification::Builder'.constantize
=> Notification::Builder
(github 上的 Rails 问题:https://github.com/rails/rails/issues/8726)
【问题讨论】:
-
Builder可能是保留名称吗? -
你能在一个新的应用程序中重现它吗?如果是这样,请在 github 上发布一个并让我知道。我会看看。在某些情况下,rails 自动加载器将无法返回正确的结果。我认为这应该是可能的......
-
@YvesSenn,感谢您的帮助:D 我在这里放了一个更简单的错误版本:github.com/rodrigues/autoload_zomg
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ruby-1.9.3