Rails ActiveRecord - 如何获取两个日期之间的记录 [英] Rails ActiveRecord - how to fetch records between two dates
本文介绍了Rails ActiveRecord - 如何获取两个日期之间的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库表中有两个日期列 - from_date
和 to_date
.
I have two date columns - from_date
and to_date
in a database table.
示例:
from_date
:2012-09-10to_date
:2012-09-30今天
:2012-09-13
如果today
的日期在from_date
和to_date
之间,我需要获取所有记录.如何使用 SQL 查询执行此操作?
I need to fetch all records, if today
's date is between from_date
and to_date
. How do I do that with a SQL query?
如果我已经加载了相应的记录,我可以很容易地决定今天的日期是否在 from_date
和 to_date
之间,但我不知道如何获取这些记录直接来自数据库表.
If I have loaded the respective record, I can easily decide, if today's date is between from_date
and to_date
, but I don't know how to fetch those records straight from the database table.
推荐答案
data = ModelName.where("today >= from_date AND today <= to_date")
这篇关于Rails ActiveRecord - 如何获取两个日期之间的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文