Warm tip: This article is reproduced from serverfault.com, please click

datetime-如何使用Ruby on Rails查找今天,昨天等的记录?

(datetime - How can I find records from today, yesterday and so on with Ruby on Rails?)

发布于 2010-08-25 22:31:29

我想查找今天用Ruby on Rails创建的所有记录,例如Posts,然后查找昨天创建的所有Post,依此类推……我该怎么办?

谢谢,

凯文

Questioner
martini-bonanza
Viewed
0
3,721 2016-03-12 05:27:54

试试这个:

#Today
Posts.find(:all, conditions: { :created_at => Date.today...Date.today + 1 })
#Yesterday
Posts.find(:all, conditions: { :created_at => Date.today - 1...Date.today })

或这样(我认为最好):

#Today
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today] )
#Yesterday
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today - 1] )