加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用可变日期搜索Rails中的日期范围

发布时间:2020-12-16 19:30:57 所属栏目:百科 来源:网络整理
导读:如何在rails活动记录中这样做? 找到所有匹配的机型(created_at本月的100天) 编辑: 好的,抱歉不准确这是我正在尝试在Rails 3.0的主动记录方式: select distinct p.ID from patients p inner join vaccines_patients vp on p.ID = vp.patient_id inner join
如何在rails活动记录中这样做?

找到所有匹配的机型(created_at本月的100天)

编辑:
好的,抱歉不准确这是我正在尝试在Rails 3.0的主动记录方式:

select 
    distinct p.ID 
from
    patients p
    inner join vaccines_patients vp on p.ID = vp.patient_id
    inner join vaccines v on v.ID = vp.VACCINE_ID
where
    month(vp.appliedAt + INTERVAL v.duration DAY) = month(now())

我想得到一个类似的查询,但使用活动记录中的哪个.

解决方法

你没有指定Rails 2或3,我并不完全确定你正在寻找什么范围,但这应该让你开始.请添加一些示例日期,并说出是否应该落入您的范围.

在Rails 2中,您可以在模型中使用named_scope.

# This range represents "created_at" values that are within 100 days on either side of today.
# Please clarify what "created_at + 100 days between this month" means if you need help
# refining this.
#
named_scope :within_range,lambda {{ :conditions => ["created_at <= ? AND created_at >= ?",Date.today + 100,Date.today - 100] }}

在Rails 3中,您将使用新的Arel范围方法的范围:

scope :within_range,lambda { where("created_at <= ? AND created_at >= ?",Date.today - 100) }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读