加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 渴望加载rails lib

发布时间:2020-12-17 04:21:35 所属栏目:百科 来源:网络整理
导读:看起来这个问题将在Rails 4中得到解决: http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/ 但在那之前,我想知道如何在我的/ lib中加载模块/类. 在IRB中,它似乎是我第一次尝试访问时按需加载: Foo::Bar.constants= []Foo::Bar::Se
看起来这个问题将在Rails 4中得到解决:
http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/
但在那之前,我想知道如何在我的/ lib中加载模块/类.

在IRB中,它似乎是我第一次尝试访问时按需加载:

Foo::Bar.constants
=> []

Foo::Bar::Service
=> Foo::Bar::Service

Foo::Bar.constants
=> [:ServiceBase,:Service]

我在该模块中有其他几个类,我的代码依赖于能够使用Foo :: Bar.const_defined查找它们吗?在运行时 – 如何确保所有Foo :: Bar的类在启动时加载?

我已经在application.rb中使用config.autoload_paths =%W(#{config.root} / lib).

解决方法

将它放在root / config / initializers / eager.rb中应该加载该文件夹中的所有.rb文件:
Dir["#{Rails.root}/lib/*.rb"].each {|file| load file}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读