You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用whenever gem运行schedule.rb报Integer无day方法问题求助

报错原因
  • schedule.rb是whenever gem专属的DSL配置文件,不能直接用ruby schedule.rb命令执行:
    • 代码中的1.day是Ruby on Rails框架Active Support模块提供的整数扩展方法,原生Ruby环境没有定义该方法
    • 直接执行该文件时既没有加载whenever的DSL解释环境,也没有加载Rails的核心扩展,所以会抛出undefined method 'day' for 1:Integer的错误
解决方法
  • 如果你使用的是Ruby on Rails项目,按以下正确流程操作即可:
    1. 确保Gemfile中已添加whenever依赖,且执行过bundle install完成安装
    2. 编写好config/schedule.rb的定时规则后,执行bundle exec whenever可以将配置转换成crontab格式语法,预览是否符合预期
    3. 确认规则无误后,执行bundle exec whenever --update-crontab即可将定时任务写入系统crontab生效
  • 如果你在非Rails项目中使用whenever,需要在schedule.rb头部手动引入Active Support的时间扩展:
    require 'active_support/core_ext/integer/time'
    # 后续再编写你的定时任务规则
    

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:15:03