问题
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