Ruby-on-rails 如何使用可变日期在 Rails 中搜索日期范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5284696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I search date range in Rails with variable date
提问by Giancarlo Corzo
How can I do this in rails active record??
我怎样才能在 Rails 活动记录中做到这一点?
find all models that match (created_at + 100 days between this month)
查找所有匹配的模型(created_at + 本月之间的 100 天)
Edit: Ok, Sorry for not be precise this what I'm trying to do in active record in Rails 3.0 way:
编辑:好的,对不起,我在 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())
I want to get a similar query but using where in active record.
我想获得类似的查询,但使用活动记录中的位置。
采纳答案by jdl
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.
您没有指定 Rails 2 或 3,我不完全确定您实际要查找的范围,但这应该可以帮助您入门。请添加一些示例日期并说明它们是否应该在您的范围内。
In Rails 2 you can use a named_scope in your model.
在 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] }}
In Rails 3, you would use a scopewith the new Arel scope methods:
在 Rails 3 中,您可以将 ascope与新的 Arel 范围方法一起使用:
scope :within_range, lambda { where("created_at <= ? AND created_at >= ?", Date.today + 100, Date.today - 100) }
回答by Cristhian Boujon
In Rails 3 you can use:
在 Rails 3 中,您可以使用:
YourModel.where(:created_at => start_date..end_date)
where start_dateand end_dateare Date class.
其中start_date和end_date是 Date 类。
回答by Matt Polito
ActiveRecordcan build a query using a BETWEENwhen it accepts a Range. This sounds like it might be more what you are looking for.
ActiveRecord可以BETWEEN在接受 a 时使用 a 构建查询Range。这听起来可能更符合您的要求。
YourModel.where(created_at: 100.days.ago..100.days.from_now)
YourModel.where(created_at: 100.days.ago..100.days.from_now)
It always seems a little simpler to do this than using >=<=in the query
这样做似乎总是比>=<=在查询中使用更简单
回答by santuxus
If I understood, you need something like this:
如果我理解,你需要这样的东西:
date = Date.today
after = date.start_of_month - 100
before = date.end_of_month - 100
YourModel.find(:all, :conditions => ['created_at > ? AND created_at < ?', after, before])
or in scope (rails 2):
或在范围内(导轨 2):
# shorter name is needed
named_scope :created_100_days_before_this_month, lambda do |date|
after = date.start_of_month - 100
before = date.end_of_month - 100
YourModel.find(:all, :conditions => ['created_at > ? AND created_at < ?', after, before])
end

