Finding by date

Hi,

Say I have a Widget that has a created_on field.

How can I find:

  • All widgets that were created last year
  • All widgets that were created during a certain month
  • All widgets that were created during a certain week
  • All widgets that were created during a certain day

Do I need to write a SQL finder for this? There could be more than a
100k widgets, so performance would be nice to have.

Thanks,
Joe

Model.find(:all, :conditions => [‘created_at < ?’, 1.year.ago]) #
Created
more than 1 year ago
Model.find(:all, :conditions => [‘created_at > ? AND created_at < ?’,
DateTime.new(2006, 3), DateTime.new(2006, 4)]) # Created in March

you get the idea…

Ian L. wrote:

Model.find(:all, :conditions => [‘created_at < ?’, 1.year.ago]) # Created
more than 1 year ago
Model.find(:all, :conditions => [‘created_at > ? AND created_at < ?’,
DateTime.new(2006, 3), DateTime.new(2006, 4)]) # Created in March

you get the idea…

Yes, I do. That’s not hard at all. Should have been obvious.

Sometimes I surprise myself with how dumb I am. >:(