升级Rails 7.1遇scheduled_at弃用警告,求解决方案
Rails 7.1升级后ActiveJob弃用警告的修复方案
升级至Rails 7.1时遇到以下无公开搜索结果的弃用警告:
DEPRECATION WARNING: Assigning a numeric/epoch value to scheduled_at is deprecated. Use a Time object instead.
警告在使用wait_until调度后台任务时触发,测试中传入的1.month.from_now明明返回Time对象,但仍触发警告。
疑似触发警告的业务代码
NotifyAudienceJob.set(wait_until: published_at).perform_later(audience.id)
触发警告的测试代码
test 'creating audiences for a scheduled article with notifications enabled should schedule notifications' do @article.update!(published_at: 1.month.from_now) assert_enqueued_with(job: NotifyAudienceJob) do post admin_news_feed_article_audiences_url(@article), params: { whole_organisation: true } end end
该警告来自ActiveJob核心代码文件active_job/lib/active_job/core.rb的第177行。
修复建议
显式转换Time类型
检查published_at字段的序列化情况,可能数据库存储或模型返回的是时间戳数值而非Time对象。在传入wait_until时显式转换为Time对象:NotifyAudienceJob.set(wait_until: published_at.to_time).perform_later(audience.id)升级至Rails 7.1.0.rc2及以上版本
该问题已在Rails 7.1.0.rc2中被官方修复,升级到该版本或后续正式版后,该弃用警告会自动消失。
内容的提问来源于stack exchange,提问作者Phil-6
相关产品推荐
相关产品推荐

