我有我的运动模型内的下列范围与无END_DATE处理:在日期范围范围
scope :dashboard_campaigns, ->(start_date, end_date, client_id) {where(client_id: client_id).where('start_date >= ? and end_date <= ?', start_date, end_date)}
此范围在指定日期范围内收集到campaigns
仪表板内使用。
有时,当用户启动一个活动中,end_date
不知道,因此nil
如果end_date
是nill,我们可以假设一个活动仍然活跃,因此应包含在最终的关系。
我只是不知道该怎么写。在尝试并且未能在范围声明中测试零值之后,我现在正在考虑将记录中的默认日期设置为从现在开始的50年,直到它由用户更新。
有没有更优雅的方式来做到这一点,我失踪了?
Postgres支持'infinity'。不知道Ruby是否支持这个,但是你可以试着'end_date <= coalesce(?,'infinity')' – 2015-03-31 06:08:19