如何搜索日期范围在Rails的可变日期 [英] How can I search date range in Rails with variable date

查看:137
本文介绍了如何搜索日期范围在Rails的可变日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做到在铁轨这种主动record¿?

How can I do this in rails active record¿?

查找符合所有车型(这个月的created_at + 100天)

find all models that match (created_at + 100 days between this month)

编辑: 好了,对不起,不是precise这就是我想要做的活动记录中的Rails 3.0的方式:

Ok, Sorry for not be precise this what I'm trying to do in active record in Rails 3.0 way:

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())

我希望得到一个类似的查询,但在那里的活动记录使用。

I want to get a similar query but using where in active record.

推荐答案

您没有指定Rails的2或3,我不完全知道的范围,你实际上是在寻找的,但这应该让你开始。请添加一些例如日期,并说他们是否掉入范围或没有。

You didn't specify Rails 2 or 3, and I'm not entirely sure what range you're actually looking for, but this should get you started. Please add some example dates and say whether they should fall into your range or not.

在轨道2,你可以在模型中使用named_scope。

In Rails 2 you can use a named_scope in your model.

# 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中,您将使用范围新阿雷尔范围的方法:

In Rails 3, you would use a scope with the new Arel scope methods:

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

这篇关于如何搜索日期范围在Rails的可变日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆