Search code examples
ruby-on-railspostgresqlactiveadminrecorddestroy

Ruby on Rails: User not deleting from the postgres database


I'm working on a ROR application which uses ActiveAdmin. I'm trying to delete a user using batch action. It is showing the message of success but in actual it is not deleting the user from database (postgres).

Here is the code of batch action -

batch_action :destroy, :confirm => "Are you sure you want to delete the user?", do |ids|
   Application::RecordsDestroy.perform_async Application::User, ids
   redirect_to user_path, :notice => "User is deleted"
   end

This is the code of Records Destroy file -

  class RecordsDestroy
    include Sidekiq::App

    def perform(res_model, ids)
      records = res_model.constantize.where(id: ids).destroy_all
      Rails.logger.info "Records for model #{res_model} have been premanently deleted"
    end
  end

Can anyone tell me what's wrong with code? Why it isn't working?

Any kind of help would be greately appreciated.


Solution

  • This appears to be a classic mistake when using Sidekiq. See best practices on job parameters.

    You must not pass an AR model or complex object directly, as this will not be serialized correctly by Sidekiq. Instead, you must pass a primitive, such as a string:

    Application::RecordsDestroy.perform_async 'Application::User', ids
    

    Related tip on debugging Sidekiq: Add

    require 'sidekiq/testing'
    Sidekiq::Testing.inline!
    

    to your application initialization (e.g. development.rb), which will make your jobs run synchronously in you main process.