how to automatically delete a record from the database in rails

后端 未结 4 473
悲哀的现实
悲哀的现实 2021-02-04 22:35

i have a rails table setup called products and its all working well, lets assume i want to auto delete a record 3 weeks after creation how do go about the code

my produc

4条回答
  •  -上瘾入骨i
    2021-02-04 22:52

    Create a rake task and run that rake task periodically. Something like:

    delete_old_products.rake:

    task delete_old_products: :environment do
      Product.where("created_at <= ?", Time.now - 3.weeks).each do |product|
        product.destroy
      end
    end
    

    You can run that from the command line with rake delete_old_products

提交回复
热议问题