我如何访问 Rails 3 应用程序中“每当”定义的常量Schedule.rb 文件的 gem
我在 application_controller.rb 文件中声明了几个常量。 有什么方法可以在 gem 的 Schedule.rb 文件中访问它们吗?
问候, 苏迪尔 CN
i have few constants declared in application_controller.rb file.
Is there any way to access them in schedule.rb file of whenever gem?
Regards,
Sudhir C.N.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当 gem 默认情况下不包含 Rails 环境。如果您想这样做,您需要在 cron 任务中加载 Rails。但是,不建议这样做,特别是如果您只需要访问某些常量的话。
我建议将常量定义移动到配置文件或初始值设定项。然后,您可以从 application_controller 和无论何时配置中都需要该文件。它将整合逻辑并让您可以访问这些常量,而无需每次调用时加载整个 Rails 应用程序。
我希望这有帮助。
The whenever gem does not include the Rails environment by default. If you wanted to do this, you would need to load Rails within your cron tasks. This is not recommended however, especially if all you need is access to some constants.
I would recommend moving your constant definitions to a config file or initializer. Then you can require that file from both your application_controller and your whenever configuration. It will consolidate the logic and give you access to those constants without having to load the entire Rails app for each invocation of whenever.
I hope that helps.