我有 3 个模型:
Class Model1
end
Class Model2
end
Class Model3
end
我有这个代码:
scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }
class << self
## Class methods for calculating searches
def created_today
today = Time.zone.now
created_between(today.beginning_of_day, today.end_of_day)
end
def created_yesterday
yesterday = Time.zone.now - 1.day
created_between(yesterday.beginning_of_day, yesterday.end_of_day)
end
def created_last_week
start_time = (Time.zone.now - 1.week).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_month
start_time = (Time.zone.now - 1.month).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_year
start_time = (Time.zone.now - 1.year).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
结尾
我需要将此代码添加到 3 个模型,但我不想在每个模型中重复代码。
我该怎么做?
非常感谢!