如何在Rails中的关联模型上配置pg_search多重搜索? [英] How to configure a pg_search multisearch on associated models in Rails?

查看:91
本文介绍了如何在Rails中的关联模型上配置pg_search多重搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将pg_search添加到Rails应用程序中.我还没有完全了解配置,因此希望在正确的方向上轻按一下.

I'm adding pg_search into a Rails app. I'm not completely understanding the configuration, and would appreciate a gentle nudge in the right direction.

首先,我已经或多或少地建立了一个多模型网站并在我的应用程序上运行.但我想将其扩展为也可以搜索关联的模型.

First, I already have a multi model site more or less set up and running on my app. But I want to extend it to also search on associated models.

例如,我有制造商,汽车,模型类.当前,如果我搜索福特",则仅返回制造商.我还要返回所有关联的汽车(属于制造商)和模型(属于汽车).

For example, I have Manufacturer, Car, Model classes. Currently if I search for "Ford", only the manufacturer is returned. I'd also like to return all the associated Cars (which belong to Manufacturer) and Models (which belong to Car).

我可以看到如何作为范围搜索

I can see how to do this as a scoped search

class Car
  pg_search_scope :manufactured_by, :associated_against => {
    :manufacturer => [:name]
  }
end

但是,如果我尝试在多重搜索中执行此操作,那么它将无法正常工作

But if I try to do this on a multisearch it doesn't work

class Car
  include PgSearch
  multisearchable :against => [:name], 
    :associated_against => {
        :manufacturer => [:name]
      }
end

它不会产生错误,只是不会拾取相关记录.

It doesn't generate an error, it simply doesn't pick up the associated records.

在我对所有这些如何融合的理解中,我感到缺少一些基本知识.如果有人能帮助我理解这一点,或为我提供良好的信息来源,我将不胜感激.我已经遍历了github和相关的Railscast上的信息,但是我仍然缺少一些东西.

I have a feeling I'm missing something fundamental in my understanding of how this all fits together. I'd really appreciate if someone could help me understand this, or point me towards a good source of info. I've been through the info on github and the related Railscast, but I'm still missing something.

推荐答案

由于多态关联在Rails和SQL中的工作方式,因此无法通过多重搜索来搜索关联记录.

It is impossible to search associated records with multisearch, due to how polymorphic associations work in Rails and SQL.

我将添加一个错误来解释这种情况,以便将来不会造成混乱.

I will add an error that explains the situation so that in the future it won't be as confusing.

很抱歉造成混乱.

您可以做的是在Car上定义一个方法,该方法返回要搜索的文本.

What you could do instead is define a method on Car that returns the text you wish to search against.

class Car < ActiveRecord::Base
  include PgSearch
  multisearchable :against => [:name, manufacturer_name]
  belongs_to :manufacturer

  def manufacturer_name
    manufacturer.name
  end
end

或更简洁地说,您可以委托:

Or to be even more succinct, you could delegate:

class Car < ActiveRecord::Base
  include PgSearch
  multisearchable :against => [:name, manufacturer_name]
  belongs_to :manufacturer
  delegate :name, :to => :manufacturer, :prefix => true
end

但是,如果您更改了Manufacturer实例的名称,则必须确保pg_search_documents表已更新,因此应将:touch => true添加到其关联中:

But you have to make sure the pg_search_documents table gets updated if you ever make a name change to a Manufacturer instance, so you should add :touch => true to its association:

class Manufacturer < ActiveRecord::Base
  has_many :cars, :touch => true
end

这样,当制造商更新时,它将在所有Car记录上调用Active Record回调,这将触发pg_search回调来更新存储在相应pg_search_documents条目中的可搜索文本.

This way it will call the Active Record callbacks on all the Car records when the Manufacturer is updated, which will trigger the pg_search callback to update the searchable text stored in the corresponding pg_search_documents entry.

这篇关于如何在Rails中的关联模型上配置pg_search多重搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆