您可以使用范围执行此操作:
class MyModel < ActiveRecord::Base
scope :two_weeks_before, lambda{|the_date| where('my_date between ? and ?', 2.weeks.until(the_date), the_date)}
scope :one_week_before, lambda{|the_date| where('my_date between ? and ?', 1.week.until(the_date), the_date)}
scope :the_day_before, lambda{|the_date| where('my_date between ? and ?', 1.day.until(the_date), the_date)}
scope :undelivered, where(:delivered => false)
end
# usage:
MyModel.two_weeks_before(Date.tomorrow).undelivered.all.each do |model|
MyMailer.notification(model).deliver
model.update_attribute(:delivered, true)
end
日期范围获取日期在 2 周前的所有模型,您可以根据需要调整端点,obv。