Search code examples
ruby-on-railsrubyelasticsearchsearchkick

overide reindex searchkick method error :NoMethodError (super: no superclass method `reindex' for #<Searchkick::RecordIndexer


We are trying to overide the reindex method of Searchkick in order to avoid reindexing in case we are on local env.

So we created a initializers/record_indexer.rb :

class Searchkick::RecordIndexer
  def reindex(options= {})
    unless Rails.env == 'local'
      super(options)
    end
  end
end

When I try to update an associated model that cause a reindexation of my 'indexed record' it throws a NoMethodError (super: no superclass method `reindex' for #<Searchkick::RecordIndexer)

I notice that searchkick has at least 3 reindex methods in:

  • Searchkick::RecordIndexer (for Foo.first.reindex I guess)
  • Searchkick::Index (for Foo.reindex I guess)
  • Searchkick::Model (for ??)

Did someone already have this kind of problematic on the #reindex method of Gem Searckick (v4.4.2)?


Solution

  • In your code you are completely replacing a method with your implementation.

    If you override a method, and want to call the original, you have two options:

    1. Store the original method with an alias

      class Searchkick::RecordIndexer
        alias_method :orig_reindex, :reindex
      
        def reindex(options={})
          unless Rails.env == 'local'
            orig_reindex(options)
          end
        end
      end
      
    2. Prepend a module

      module YourPatch
        def reindex(options={})
          unless Rails.env == 'local'
            super # no need to specify args if it's just pass-through
          end
        end
      end
      
      Searchkick::RecordIndexer.prepend(YourPatch)