Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用基类搜索多搜索STI_Ruby On Rails_Pg Search - Fatal编程技术网

Ruby on rails 使用基类搜索多搜索STI

Ruby on rails 使用基类搜索多搜索STI,ruby-on-rails,pg-search,Ruby On Rails,Pg Search,我正在建立一个审查网站,用户可以审查多种类型的业务。企业使用STI存储,用户有自己的模型。我想实现一个单一的搜索栏,人们可以在这里搜索用户或企业。输入pg_搜索 module Business class Venue < Base include PgSearch multisearchable :against => [:name, :address, :locality, :region] end end module Business class

我正在建立一个审查网站,用户可以审查多种类型的业务。企业使用STI存储,用户有自己的模型。我想实现一个单一的搜索栏,人们可以在这里搜索用户或企业。输入pg_搜索

module Business
  class Venue < Base
    include PgSearch
    multisearchable :against => [:name, :address, :locality, :region]
  end
end

module Business
  class Service < Base
    include PgSearch
    multisearchable :against => [:name, :address, :locality, :region]
  end
end
返回

(0.3ms)  INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
SELECT 'Business::Base' AS searchable_type, ...

有没有办法在“我的模型”中指定可搜索的类型,或者有什么原因使其无法工作?

您可以将第二个参数(布尔值)传递给
重建
方法,该方法将阻止它删除您的所有记录

def rebuild(model, clean_up=true)                                                                                                                                                                           
  model.transaction do                                                                                                                                                                                      
    PgSearch::Document.where(:searchable_type => model.base_class.name).delete_all if clean_up                                                                                                              
    Rebuilder.new(model).rebuild                                                                                                                                                                            
  end                                                                                                                                                                                                       
end
因此,如果您有多个继承模型,您可以执行以下操作:

PgSearch::Document.where(searchable_type: 'Business::Base').delete_all
PgSearch::Multisearch.rebuild(Business::Venue, false)
PgSearch::Multisearch.rebuild(Business::Service, false)
PgSearch::Document.where(searchable_type: 'Business::Base').delete_all
PgSearch::Multisearch.rebuild(Business::Venue, false)
PgSearch::Multisearch.rebuild(Business::Service, false)