Kadismile
Kadismile

Reputation: 208

how to automatically delete a record from the database in rails

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 product db is as follows

class Products < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.text :description
      t.string :price
      t.string :name
      t.string :contact
      t.attachment :photo
      t.string :slug
      t.integer :user_id

      t.timestamps
    end
    add_index :products, :user_id
  end
end

Upvotes: 2

Views: 6678

Answers (4)

egyamado
egyamado

Reputation: 1121

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.

Upvotes: 7

rails_id
rails_id

Reputation: 8220

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

Upvotes: 3

Jyothu
Jyothu

Reputation: 3144

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

Upvotes: 3

Michael Lawrie
Michael Lawrie

Reputation: 1554

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

Upvotes: 0

Related Questions