代码之家  ›  专栏  ›  技术社区  ›  dav

在轨道上旋转一天的产品

  •  1
  • dav  · 技术社区  · 15 年前

    我正在创建一个类似于臭名昭著的Woot的网站,有一个特别的方式:我想每天以一个的速率在产品间轮换。我正在使用ror,希望动态选择一个产品,而不必每天输入一个。因此,要求如下:

    1)每天购买新产品
    2)从表中动态选择
    3)确保不要两次选择同一产品。

    就是这样。在products表中,我当前有一个名为 listed 如果产品已列出,则会标记。我的问题是,我是否应该运行某种数据库作业来选择当天的产品,或者是否可以通过某种方式来散列日期,并想出一个ID,在日期更改之前应用程序将显示该ID。

    谢谢你的时间。

    1 回复  |  直到 9 年前
        1
  •  2
  •   jerhinesmith    9 年前

    就我个人而言,我会保持简单,按计划工作。更具体地说,我可能会使用 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