Я не знаю о драгоценном камне, хотя было бы очень здорово создать его. Тем не менее, это, вероятно, поможет вам:
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's 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
Это даст вам некоторые базовые возможности, такие как Thing.this_month ... плюс динамический искатель, который позволит вам делать что-то вроде Thing.last_90_days и работать с любым числом (ВНИМАНИЕ: я немного из method_missing n00b, код работал для меня, но, возможно, кто-то может проверить его еще раз).
К вашему сведению, я предполагаю, что Rails 3 здесь.