Topic: Date query

Hey!

I have a table in my db that holds events, with the date saved as yyyy-mm-dd. I want to be able to extract this weeks events with one query.

I have found Date::cweek(), but I don't get how to use it...

Re: Date query

If you want the events that will happen in the next 7 days you can do:

events = Event.find(:all, :conditions => ['happens_on >= ? AND happens_on < ?', Date.today, Date.today+7])

Or to get the events that will be happening from now until Sunday:

events = Event.find(:all, :conditions => ['happens_on >= ? AND happens_on < ?', Date.today, Date.today+7-Date.today.wday])

Untested, and you will probably have to adjust it a bit to get it to work in your situation.

Last edited by ryanb (2006-07-31 10:24:04)

Railscasts - Free Ruby on Rails Screencasts

Re: Date query

Works great! Thanks!