how to automatically delete a record from the database in rails

后端 未结 4 474
悲哀的现实
悲哀的现实 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条回答
  •  粉色の甜心
    2021-02-04 22:48

    You need to create a cron Job, Which has to run each day at a particular time. To set up a Cron job you can use the Gem WHENEVER

    And the Query should be like

    Product.where("created_at <= ?", Time.now - 3.weeks).destroy_all
    

提交回复
热议问题