Rails ActiveRecord 日期之间 [英] Rails ActiveRecord date between
本文介绍了Rails ActiveRecord 日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要查询一天的评论.该字段是标准时间戳的一部分,是 created_at
.所选日期来自 date_select
.
I need to query comments made in one day. The field is part of the standard timestamps, is created_at
. The selected date is coming from a date_select
.
我如何使用 ActiveRecord
来做到这一点?
How can I use ActiveRecord
to do that?
我需要类似的东西:
"SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'"
推荐答案
请注意,当前接受的答案在 Rails 3 中已弃用.您应该改为这样做:
Just a note that the currently accepted answer is deprecated in Rails 3. You should do this instead:
Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day)
或者,如果您想或必须使用纯字符串条件,你可以这样做:
Or, if you want to or have to use pure string conditions, you can do:
Comment.where('created_at BETWEEN ? AND ?', @selected_date.beginning_of_day, @selected_date.end_of_day)
这篇关于Rails ActiveRecord 日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文