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

如何在Rails作用域中用Arel查询complete_by≤当前日期的记录

Ruby on Rails 作用域修改方案

直接给你修改好的作用域代码,用Arel实现complete_by日期小于等于当前日期的筛选:

scope :index_overdue, -> { 
  where(active: true, end_date: nil)
    .where(arel_table[:complete_by].lte(DateTime.current))
    .order(arel_table[:complete_by], arel_table[:latest_update_date], arel_table[:id]) 
}

关键说明:

  • 用arel_table[:complete_by]获取对应字段的Arel节点,调用.lte()方法传入DateTime.current,就等价于SQL里的complete_by <= 当前日期
  • 单独拆分一个where来写Arel条件,比混在哈希条件里更清晰,也贴合你想用Arel的需求
  • 如果项目有时区配置,建议用Time.zone.today或者Time.zone.now适配时区,避免时间不一致的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 17:25:58