簡體   English   中英

MetaSearch Gem使用Tire Gem覆蓋搜索方法

[英]MetaSearch Gem overrides the search method with the Tire Gem

我已經向使用Rails構建並使用active_admin gem的現有web應用程序添加了全文搜索數據庫。 全文數據庫建立在彈性搜索基礎上,並使用Tire寶石。 active_admin gem具有定義Model.search方法的metasearch gem的依賴項要求。

問題在於metasearch gem會覆蓋輪胎搜索gem的搜索方法,而我似乎無法將輪胎gem中的搜索方法重新別名到模型中。 有人知道我該怎么做嗎?

-解決方案-

更新:解決方案是設置一個初始化程序以添加以下方法:

def search_for(*args,&block)
  tire.__send__(:search, *args, &block)
end

我想出了一個可行的解決方案。 基本上,您只需要將搜索方法設置為search_for而不是search

在應用程序/幫助器中創建一個名為tire_helper.rb的幫助器文件。

module TireHelper

  def search_for(*args,&block)
    tire.__send__(:search, *args, &block)
  end

end

對於每個使用輪胎的模型,請使用以下命令:

class Model < ActiveRecord::Base

  extend TireHelper
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    # your mappings
  end

end

現在,您可以使用以下方法在模型上正常搜索:

# with a string
query = Model.search_for('string')

# or with a block
query = Model.search_for do
  #any of the same block stuff that Tire.search provides
end

或者,您可以在沒有任何幫助的情況下使用MyModel.tire.search("string")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM