就我个人而言,我会保持简单,按计划工作。更具体地说,我可能会使用
whenever
gem包装一个cron作业,每天午夜运行一次。然后您可以在
Product
选择当前“当日产品”的模型。
这样的方法应该有效:
产品RB
class Product < ActiveRecord::Base
# Return the current product of the day
def self.product_of_the_day
where(listed: true).first
end
# Set up the product of the day
def self.setup_product_of_the_day
current_product = self.product_of_the_day
product_ids = where.not(id: current_product.id).pluck(:id)
next_product = self.find(product_ids.sample)
current_product.update_attribute(:listed, false)
next_product.update_attribute(:listed, true)
end
end
调度表
every 1.day do
runner "Product.setup_product_of_the_day"
end