Problems using pg_search with a polymorphic association

落爺英雄遲暮 提交于 2019-12-08 09:10:27

问题


I have the following scope for my model:

class Cloth < ActiveRecord::Base
  include Ownerable
  has_many :cloth_tags, :dependent => :destroy

  pg_search_scope :full_search,
    associated_against: {
      cloth_tags: [:name, :brand_name]
    },
    against: [:name, :description],
    ignoring: :accents,
    using: {
      tsearch: {
        dictionary: "spanish",
        any_word: true
      }
    }

So if I call something like Cloth.full_search('shirt') works fine, but if I add owner: [:name] to the associated_against hash, it throws NameError: uninitialized constant Cloth::Owner. Nedless to say that the owner relationship in normal circumstances is working. In any case is defined in a module like this:

module Ownerable

  extend ActiveSupport::Concern

  included do
    belongs_to :owner, :polymorphic => true
  end

Any clue what thing could be? Thanks in advance


回答1:


I'm the author and maintainer of pg_search.

Unfortunately, it's not possible to traverse a polymorphic association in this direction in pure SQL, so doing that search is not possible with pg_search.

One thing you could do is calculate the text from the other records and cache it to a column on the Cloth table, then search against that instead. You'd have to be careful to update it whenever either the polymorphic foreign key changes on Cloth or the content changes on the Owner record.

Hopefully I can improve the error message so that it's not so confusing. Thanks for pointing this out.



来源:https://stackoverflow.com/questions/15364422/problems-using-pg-search-with-a-polymorphic-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!