查找日期时间与今天日期匹配的记录 - Ruby on Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5943872/
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
Find Records with Datetime that match today's date - Ruby on Rails
提问by Robert B
I have a table of deals and need to find records where the date matches today's date.
我有一张交易表,需要查找日期与今天日期相匹配的记录。
From the rails console, the date field I need to match looks like this. I've assigned a record to deal for testing.
从 rails 控制台,我需要匹配的日期字段看起来像这样。我已经分配了一个记录来处理测试。
ruby-1.9.2-p0 > deal.start
=> Tue, 10 May 2011 00:00:00 UTC +00:00
If I try to find any records matching the start date with today like this I get nil
如果我尝试像这样找到与今天开始日期匹配的任何记录,我会得到零
ruby-1.9.2-p0 > Deal.find_by_start(Date.today)
=> nil
then I thought I could match by converting Date.today to a datetime.
然后我想我可以通过将 Date.today 转换为日期时间来匹配。
ruby-1.9.2-p0 > Date.today.to_datetime
=> Tue, 10 May 2011 00:00:00 +0000
ruby-1.9.2-p0 > Deal.find_by_start(Date.today.to_datetime)
=> nil
How can I get this to work? I'm using Rails 3.
我怎样才能让它发挥作用?我正在使用 Rails 3。
Edit: I thought about converting them both to a consistent format which will works but not when trying to using the find_by_start method
编辑:我想过将它们都转换为一致的格式,这将起作用,但在尝试使用 find_by_start 方法时不起作用
ruby-1.9.2-p0 > deal.start.strftime("%a, %e %B %Y") == Date.today.strftime("%a, %e %B %Y")
=> true
ruby-1.9.2-p0 > Deal.find_by_start.strftime("%a, %e %B %Y") == Date.today.strftime("%a, %e %B %Y")
NoMethodError: undefined method `strftime' for nil:NilClass
回答by Ilya
Rails 5.1has introduced Date#all_dayhelper, which returns a range object for a given date, so you can just write:
Rails 5.1引入了Date#all_day帮助器,它返回给定日期的范围对象,所以你可以只写:
Deal.where(start: Date.today.all_day)
You can also use SQL DATE()function for your goal, e.g.:
您还可以将 SQLDATE()函数用于您的目标,例如:
@deals = Deal.where('DATE(start) = ?', Date.today)
回答by tadman
Keep in mind a DateTime holds a date and a time, so you're looking for records that have a precise value, not just the same day.
请记住,DateTime 包含日期和时间,因此您正在寻找具有精确值的记录,而不仅仅是同一天。
You can do this one of two ways. You can either select the range of time from the beginning of the day to the end, or you can create a datecolumn to help group your data better.
您可以通过以下两种方式之一执行此操作。您可以选择从一天开始到结束的时间范围,也可以创建一个date列来帮助更好地对数据进行分组。
The range version looks like this:
范围版本如下所示:
@deals = Deal.where('start BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day).all
The other one requires creating a new start_datecolumn in your table and populating it with the dates accordingly. You can index this date and have your queries run much faster, as range selections aren't always quick on large sets of data.
另一个需要start_date在表中创建一个新列并相应地用日期填充它。您可以将此日期编入索引并使查询运行得更快,因为在大量数据集上选择范围并不总是很快。
回答by Peter
For those still on Rails 4:
对于那些仍然在 Rails 4 上的人:
To augment not_a_patch's answer above, I would use:
为了增加上面 not_a_patch 的答案,我会使用:
Deal.where(start: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
because Time.zonewill use UTC (which is how your date time field is stored) while DateTime.nowwill not.
因为Time.zone将使用 UTC(这是您的日期时间字段的存储方式),而DateTime.now不会使用。
> DateTime.now
=> Fri, 08 Sep 2017 11:28:21 -0400
> Time.zone.now
=> Fri, 08 Sep 2017 15:29:53 UTC +00:00

