针对日期字段查询日期时间字段的最佳方法是什么? [英] What is the best way to query a datetime field against date fields?
问题描述
我只想知道当你有一个日期时间字段时你们在做什么,即:<代码>
<块引用><块引用>Game.first.starts_at
=> 星期四,2009 年 12 月 3 日 20:20:00 美国东部时间 -05:00
并且您想针对日期字段查询它:<代码>
<块引用><块引用>3.days.from_now.to_date.to_s(:db)
=>2009-12-03"
目前我通过使用 BETWEEN
运算符来完成这项工作,但我想知道更多解决此问题的方法.提前致谢.
我不关心 date.to_s(:db)
转换.使用纯 Date
和 Datetime
对我来说效果很好.
Game.first(:conditions => ["starts_at <= ?", 3.days.from_now.to_date])
I just want to know what you guys are doing when you have a datetime field, ie:
Game.first.starts_at
=> Thu, 03 Dec 2009 20:20:00 EST -05:00
And you want to query it against a date field:
3.days.from_now.to_date.to_s(:db)
=> "2009-12-03"
Currently I make this work by using the BETWEEN
operator, but I would like to know more approachs to solve this problem. Thanks in advance.
I don't bother with the date.to_s(:db)
conversion. Working with pure Date
and Datetime
works fine for me.
Game.first(:conditions => ["starts_at <= ?", 3.days.from_now.to_date])
这篇关于针对日期字段查询日期时间字段的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!