【问题标题】:overide reindex searchkick method error :NoMethodError (super: no superclass method `reindex' for #<Searchkick::RecordIndexer覆盖重新索引 searchkick 方法错误:NoMethodError(超级:#<Searchkick::RecordIndexer 没有超类方法 `reindex'
【发布时间】:2021-06-08 13:20:08
【问题描述】:

我们正在尝试覆盖 Searchkick 的重新索引方法,以避免在本地环境中重新索引。

所以我们创建了一个 initializers/record_indexer.rb :

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

当我尝试更新导致我的“索引记录”重新索引的关联模型时,它会引发 NoMethodError(超级:#<:recordindexer>

我注意到 searchkick 在以下方面至少有 3 个重新索引方法:

  • Searchkick::RecordIndexer(我猜是 Foo.first.reindex)
  • Searchkick::Index(我猜是 Foo.reindex)
  • Searchkick::Model(用于 ??)

有人在 Gem Searckick (v4.4.2) 的 #reindex 方法上遇到过这种问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch searchkick


    【解决方案1】:

    在您的代码中,您完全用您的实现替换了一个方法。

    如果你重写一个方法,并想调用原来的方法,你有两个选择:

    1. 用别名存储原始方法

      class Searchkick::RecordIndexer
        alias_method :orig_reindex, :reindex
      
        def reindex(options={})
          unless Rails.env == 'local'
            orig_reindex(options)
          end
        end
      end
      
    2. 添加一个模块

      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)
      

    【讨论】:

    • 第一个选项更适合我的需要,因为 gem Searchkick 自动在索引模型上使用 .reindex。因为我自己从不使用 Searchkick::RecordIndexer 类,所以我更喜欢覆盖它。不知道#alias_method,但看起来我应该知道!
    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多