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

时间:2012-02-20 04:37:56

标签: ruby-on-rails ruby rubygems

我已经将全文搜索数据库添加到我使用Rails构建的现有webapp并使用active_admin gem。全文数据库建立在弹性搜索之上,并使用Tire gem。 active_admin gem具有metasearch gem的依赖性要求,它定义了Model.search方法。

问题是元搜索宝石覆盖了轮胎搜索宝石的搜索方法,我似乎无法将轮胎宝石中的搜索方法替换回模型。有谁知道我怎么能这样做?

- 解决方案 -

更新:解决方案是设置初始化程序以添加以下方法:

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

2 个答案:

答案 0 :(得分:2)

我想出了一个有效的解决方案。基本上,您只需将搜索方法设置为search_for而不是search

在app / helpers中创建名为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

答案 1 :(得分:2)

或者您可以在没有任何帮助的情况下使用MyModel.tire.search("string")