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

On Jan 13, 2008 10:05 PM, Vapor … [email protected]
wrote:

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

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

2008/1/13, Ryan B. :

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.

On Jan 13, 2008, at 5:41 AM, Ryan B. wrote:

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

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 could make that condition shorter by referring to the times as
2.weeks.ago and 1.week.ago. :slight_smile:

RSL

2008/1/13, George B. [email protected]:

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.