Search code examples
ruby-on-railsactiveadminpg-search

activeadmin destroy batch action and pg_search rebuild


I am working on a Rails web app. I have records in a model, called AcademicPaper that I deleted using the batch action destroy functionality that comes with activeadmin. Right after I did that, I noticed that there was an error of not found ActiveRecord for the AcademicPaper model in the controller method that utilized pg_search gem.

I realized that the pg_search was not synched with ActiveRecords. Therefore, the solution was for me to run PgSearch::Multisearch.rebuild(AcademicPaper) in the rails console, which I did, which worked.

My question is: how can I avoid this error in a production environment and maybe rebuild the pg_search index every time an active_admin batch action occurs?


Solution

  • Straight from Active Admin Documentation -

    If you want, you can override the default batch action to do whatever you want:

    ActiveAdmin.register Post do
      batch_action :destroy do |ids|
        super
        PgSearch::Multisearch.rebuild(AcademicPaper) 
      end
    end