Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 application_controller.rb 文件中声明的常量很少。有没有办法在任何时候 gem 的 schedule.rb 文件中访问它们?
问候, Sudhir CN
默认情况下,无论何时 gem 不包含 Rails 环境。如果你想这样做,你需要在你的 cron 任务中加载 Rails。但是,不建议这样做,尤其是当您只需要访问某些常量时。
我建议将您的常量定义移动到配置文件或初始化程序。然后,您可以从 application_controller 和您的 when 配置中要求该文件。它将整合逻辑并让您访问这些常量,而无需为每次调用加载整个 Rails 应用程序。
我希望这会有所帮助。