Event.find(:all, :conditions =>
"((strftime('%m%d', ends_at) <= strftime('%m%d', starts_at)) AND (strftime('%m%d', 'now') <= strftime('%m%d', ends_at)))
OR
((strftime('%m%d', starts_at) <= strftime('%m%d', ends_at)) AND (strftime('%m%d', 'now') <= strftime('%m%d', ends_at)) AND (strftime('%m%d', 'now') >= strftime('%m%d', starts_at)))
OR
((strftime('%m%d', ends_at) <= strftime('%m%d', starts_at)) AND (strftime('%m%d', starts_at) <= strftime('%m%d', 'now')))")
Refactorings
No refactoring yet !
halogenandtoast
October 30, 2008, October 30, 2008 19:45, permalink
This was a pain to figure out, but this should work
Event.find(:all, :conditions =>
"(strftime('%m%d', 'now') between strftime('%m%d', starts_at) and strftime('%m%d', ends_at)) or (strftime('%m%d', ends_at) <= strftime('%m%d', starts_at))")
Users can add yearly recurring events with a starts_at and ends_at field, specifying start/end day/month. I have to find the current events, for which I use a scheme as specified here: http://gist.github.com/4692
I am using SQLite3, and this is one hell of an ugly piece of code. Give it a shot!