简体   繁体   English

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

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

I've added a fulltext searching database to my existing webapp that is built with Rails and uses the active_admin gem. 我已经向使用Rails构建并使用active_admin gem的现有web应用程序添加了全文搜索数据库。 The fulltext database is built on elastic search and uses the Tire gem. 全文数据库建立在弹性搜索基础上,并使用Tire宝石。 The active_admin gem has a dependency requirement of the metasearch gem which defines the Model.search method. active_admin gem具有定义Model.search方法的metasearch gem的依赖项要求。

The problem is that the metasearch gem overrides the search method for the tire search gem and I can't seem to alias the search method in the tire gem back into the model. 问题在于metasearch gem会覆盖轮胎搜索gem的搜索方法,而我似乎无法将轮胎gem中的搜索方法重新别名到模型中。 Does anyone know how I can do this? 有人知道我该怎么做吗?

-- Solution -- -解决方案-

Update: The solution is to setup an initializer to add the following method: 更新:解决方案是设置一个初始化程序以添加以下方法:

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

I've come up with a working solution. 我想出了一个可行的解决方案。 Basically, you need to just set the search method to be called search_for instead of search . 基本上,您只需要将搜索方法设置为search_for而不是search

Create a helper file called tire_helper.rb within app/helpers. 在应用程序/帮助器中创建一个名为tire_helper.rb的帮助器文件。

module TireHelper

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

end

And for each model that uses tire then use this: 对于每个使用轮胎的模型,请使用以下命令:

class Model < ActiveRecord::Base

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

  mapping do
    # your mappings
  end

end

You can now search normally on your model(s) with the following method: 现在,您可以使用以下方法在模型上正常搜索:

# 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