how to automatically delete a record from the database in rails

后端 未结 4 472
悲哀的现实
悲哀的现实 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
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-04 22:54

    There are different ways on how to handle this task. Check 2 options below.

    Option 1 - whenever gem

    You would setup a task using whenever which runs for example every 30 days.

    1- Generate task as following:

    rails g task posts delete_30_days_old

    2- Create a ruby file in your application

    # lib/tasks/delete_old_records.rb
    namespace :posts do
      desc "Delete records older than 30 days"
      task delete_30_days_old: :environment do
        Post.where(['created_at < ?', 30.days.ago]).destroy_all
      end
    end
    

    Option 2 - sidekick and Sidetiq gems

    # in app/workers/clean_posts.rb
    class CleanPosts
      include Sidekiq::Worker
      include Sidetiq::Schedulable
    
      recurrence { monthly }
    
      def perform
        Post.recent.destroy_all
      end
    end
    

    and

    # /models/post.rb
    class Post < ApplicationRecord
      scope :recent, -> { where('created_at >= :thirty_days_ago', thiryty_days_ago: Time.now - 30.days) }
    end
    

    Since Ruby is about beauty, move the where clause to your model, where can reuse it.

    These options will remove old posts from your DB and they will no longer be accessible by your application.

    0 讨论(0)
  • 2021-02-04 22:58

    If you want automatically an action, you should use cron job in your app. I have an app using clockwork gem for schedule some process on background.

    Step by step :

    Put this on your Gemfile

    gem 'clockwork'
    gem 'foreman'
    

    Create file clock.rb on folder app

    require './config/boot'
    require './config/environment'
    require 'clockwork'
    
    module Clockwork
     every(1.day, 'delete.product', :at => '00:00') {
       Product.where("created_at >= ?", 3.week.ago.utc).destroy_all
     }
    end
    

    Running test clockwork

    clockwork app/clock.rb
    

    Example, this is my app using clockwork with delete automatically user every 30.seconds :

    C:\Sites\multiple>clockwork app/clock.rb
    I, [2013-06-12T13:36:14.380239 #2356]  INFO -- : Starting clock for 1 events: [
    delete.user ]
    I, [2013-06-12T13:36:14.380239 #2356]  INFO -- : Triggering 'delete.user'
    I, [2013-06-12T13:36:44.784978 #2356]  INFO -- : Triggering 'delete.user'
    

    Or If you want running clockwork with the app, you cold using foreman looks like :

    foreman start
    

    Note : running foreman you should install foreman on your machine

    0 讨论(0)
提交回复
热议问题