如何更改 Rails 中初始化程序的加载顺序? [英] How do I change the load order of initializers in Rails?

查看:29
本文介绍了如何更改 Rails 中初始化程序的加载顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个初始化程序,可以从 yaml 文件加载配置设置.我需要在其他初始化程序中使用这些设置.需要它们的初始化程序看不到这些设置.我认为正在发生的是设置加载太晚了.如何保证我的配置初始值设定项首先加载?让初始值设定项依赖于另一个是不正常的吗?

I have an initializer that loads configuration settings from a yaml file. I need to use these settings in other initializers. The settings are not being seen by the initializers that need them. What I think is happening is the settings are getting loaded too late. How do I guaranty that my configuration initializer gets loaded first? Is it un-rails like to have initializers depend on another?

谢谢!

推荐答案

将初始化器重命名为 01_name.rb,这将强制它之前按字母顺序加载.

Rename the initializer to 01_name.rb, that will force it to load alphabetically previously.

编辑

引用官方Rails 配置指南(感谢zetetic 提示):

To quote the official Rails Guide for configuration (thanks zetetic for the tip):

如果你有任何顺序依赖你的初始值设定项,你可以控制按命名加载顺序.例如,01_critical.rb 将在之前加载02_normal.rb.

If you have any ordering dependency in your initializers, you can control the load order by naming. For example, 01_critical.rb will be loaded before 02_normal.rb.

这篇关于如何更改 Rails 中初始化程序的加载顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆