How to get all the records of previous week?

I have a field in Post called updated_at which of DATETIME type. I want to get all the posts which were updated last week. How do I do that?

Thanks

Post.find(:conditions => [“updated_at BETWEEN ? AND ?”,Time.now-2.weeks,Time.now-1.week])

2008/1/13, Ryan Bigg :

Post.find(:conditions => ["updated_at BETWEEN ? AND ?",Time.now-2.weeks,Time.now-1.week])

Post.find :all, :conditions => { :updated_at => 2.weeks.ago..1.week.ago }

should work.

   -- Jean-François.

You could make that condition shorter by referring to the times as 2.weeks.ago and 1.week.ago. :slight_smile:

RSL

I took it to mean he was asking for "last week," as on the calendar. So no matter if today is Monday or Thursday, "last week" would still refer to the same period of time. Just as 'last month' would right now mean December.

Anyway, I was hoping to find an elegant answer to that in this thread, as the way I do that stuff is downright ugly.

You can look at :

Time.now.beginning_of_week Date.today.beginning_of_week

You can also write : Date.today.beginning_of_month and so on.

HTH,

   -- Jean-François.