1

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

Alter Lagos
  • 12,090
  • 1
  • 70
  • 92

1 Answers1

4

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.

Grant Hutchins
  • 4,275
  • 1
  • 27
  • 32
  • can you please help me out here :) http://stackoverflow.com/questions/19398866/how-to-link-pgseaerch-results-to-index-page-in-nested-resources – Serge Pedroza Oct 16 '13 at 16:15
  • Thank you for pg_search, @nertzy :-) I don't get why it's not possible to traverse a polymorphic association in pure SQL. I'm trying to brainstorm here so please let me know if something I'm saying is wrong. Can we not left join on `ownerable_id = clothes.id and ownerable_type = 'Cloth'` in the example above? We can get the model's associations with `Model.reflections` and can add to your `ForeignColumn` a 'join_on' method which could return `foos.id = blahs.id` or `foos.id = blahs.ownerable_id and blahs.ownerable_type = 'foos'`, depending on association type. – Abdo Oct 31 '14 at 10:16
  • Hi, any news here? – mmike Oct 07 '16 at 13:49