views:

32

answers:

1

Hi,

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

+5  A: 

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]
floatless