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

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

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

I want to find all records, say Posts, created today with Ruby on Rails, then all Posts created yesterday, and so on… how should I do?

Thank you,

Kevin

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

Try this:

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

Or this (preferable, in my opinion):

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