Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Finding recent data in database (See related posts)

This shows how to find recent data from last hour, day, week, month, whatever

class Post < ActiveRecord::Base
  def self.find_latest(time = nil)
    r = %w( hour day week month year )
    if r.include?(time)
      self.find :all, :conditions => ['created_at > ?', 1.send(time).ago]
    else
      self.find :all
    end
  end
end

Post.find_latest('day')
Post.find_latest('week')
Post.find_latest('year')

You need to create an account or log in to post comments to this site.


Click here to browse all 4834 code snippets

Related Posts