轨道之间的ActiveRecord日期轨道、日期、ActiveRecord

2023-09-08 15:40:43 作者:如初.

我需要查询在一天提出的意见。本场是标准时间戳的一部分,是created_at。选定的日期是从date_select到来。我如何使用ActiveRecord来做到这一点?

我需要财产以后这样的:

 SELECT * FROM评论WHERE created_at'之间2010-02-03 00:00:00和23:59:59 2010-02-03'
 

解决方案

只是说明了目前公认的答案是德precated用Rails 3。你应该这样做,而不是:

  Comment.where(:created_at => @ selected_date.beginning_of_day .. @ selected_date.end_of_day)
 

Rails Active Record Associations 笔记 一

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. How can I use ActiveRecord to do that?

I need somthing like:

"SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'"

解决方案

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)