问题描述
我试图查找'created_at'属性最新记录 - 数据库列类型是'日期时间'和我使用jQuery的从用户界面的DatePicker
我的网址是这样的:本地主机:3000 / users_supported selected_date = 2012-10-31
到目前为止,我做得不错:)我在控制器的查询看起来是这样的:
@support_histories = current_agent.support_histories.where(:created_at => Date.parse(PARAMS [:selected_date]))
如何通过日期只能从日期时间数据库列
正确提取记录我试图在Rails的控制台这样做,但没有运气:
SH = SupportHistory.where(:created_at => DateTime.parse('2012-10-31'))
的sh = SupportHistory.where(:created_at => Date.parse('2012年10月31日))
SH = SupportHistory.where(:created_at => DateTime.strptime('2012-10-31','%Y-%M-%D'))
我有记录,如果我不喜欢下面提到的,但是这不是对我很有用,因为我试图找到通过'日期'的记录没有被日期时间
SH = SupportHistory.where(:created_at =>'2012-10-31 19点49分57秒)
selected_date = Date.parse(PARAMS [:selected_date])
#这将查找记录在给定日期00:00:00和23:59:59之间
SH = SupportHistory.where(
:created_at => selected_date.beginning_of_day..selected_date.end_of_day)
时区可能是你需要考虑一个问题,但这应该工作,如果你所有的时间都在同一个时区。
I am trying to find records by 'created_at' date - the database column type is 'datetime' andI am using the UI DatePicker from jQuery
my url look like this: "localhost:3000/users_supported?selected_date=2012-10-31"
So far i am doing good :) and my query in controller looks like this:
@support_histories = current_agent.support_histories.where(:created_at => Date.parse(params[:selected_date]))
How to properly extract records by 'date' only from the 'datetime' db column
I tried doing this in Rails Console, but no luck there:
sh = SupportHistory.where(:created_at => DateTime.parse('2012-10-31'))
sh = SupportHistory.where(:created_at => Date.parse('2012-10-31'))
sh = SupportHistory.where(:created_at => DateTime.strptime('2012-10-31', '%Y-%m-%d'))
I got records if i do like mentioned below, but that's not useful to me as i am trying to find record by 'date' not by 'DateTime'
sh = SupportHistory.where(:created_at => '2012-10-31 19:49:57')
selected_date = Date.parse(params[:selected_date])
# This will look for records on the given date between 00:00:00 and 23:59:59
sh = SupportHistory.where(
:created_at => selected_date.beginning_of_day..selected_date.end_of_day)
Time Zones may be a concern you need to look into, but this should work if all your times are in the same time zone.
这篇关于导轨的ActiveRecord查找日期/搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!