I do not know about stone, although it would be great to create it. In this case, you will probably receive:
class Thing < ActiveRecord::Base # Some date-related scopes scope :today, where("created_at > ?", Time.now.beginning_of_day) scope :yesterday, where("created_at < ? and created_at > ?", 1.day.ago.end_of_day, 1.day.ago.beginning_of_day) scope :this_month, where("created_at > ?", Time.now.beginning_of_month) scope :last_month, where("created_at < ? and created_at > ?", 1.month.ago.end_of_month, 1.month.ago.beginning_of_month) scope :this_year, where("created_at > ?", Time.now.beginning_of_year) ### # All your model stuff goes here ### # Let get real tricky with method_missing # and allow Thing.last_X_days for work for any value of X private def self.method_missing(method, *args, &block) if method.to_s =~ /^last_(\d+)_days$/i days = method.to_s.match(/^last_(\d+)_days$/i) return self.where("created_at > ?", days[1].to_i.days.ago.beginning_of_day) else super end end end
This will give you some basic areas, such as Thing.this_month ... plus a dynamic finder that will allow you to do something like Thing.last_90_days and work for any number (WARNING: I have a little method_missing n00b, the code worked for me, but, maybe someone can check it twice).
FYI I accept Rails 3 here.
source share