Skip to content

associated_against with tsvector_column #359

@cireficc

Description

@cireficc

I have tried every which way and cannot get this pg_search scope to work. The following does work:

Translation

class Translation < ActiveRecord::Base
  belongs_to  :source_phrase,
              class_name: "Phrase",
              foreign_key: "source_id"
  belongs_to  :destination_phrase,
              class_name: "Phrase",
              foreign_key: "destination_id"

  include PgSearch
  pg_search_scope :search_full_text, :associated_against =>
      {:source_phrase => :text}
end

Phrase

class Phrase < ActiveRecord::Base
  
  self.table_name = "new_phrases"

  has_many    :translations,
              class_name: "Translation",
              foreign_key: "source_id"
  has_many    :source_phrases,
              through: :translations
  has_many    :inverse_translations,
              class_name: "Translation",
              foreign_key: "destination_id"
  has_many    :destination_phrases,
              through: :inverse_translations
end

In the above I am not trying to use a ts_vector column on the associated table - I am just using the text field (which is obviously incredibly slow). If I try the following (in translation.rb):

pg_search_scope :search_full_text,
                  :associated_against => {
                      :source_phrase => :text,
                      :using => {
                          :tsearch => {
                              :tsvector_column => "tsv_text"
                           }
                       }
                  }

I get the error: NoMethodError: undefined method 'table_name' for nil:NilClass. Well that makes sense because using isn't part of Association. If I change it up slightly:

pg_search_scope :search_full_text,
                  :associated_against => {
                      :source_phrase => :text
                  },
                  :using => {
                      :tsearch => {
                          :tsvector_column => "tsv_text"
                      }
                  }

I get the error column translations.tsv_text does not exist, which makes sense, because the column I want is phrases.tsv_text. I can't specify the table name in the tsearch option, so I don't know what to do.

How do I use the :tsvector_column option for an associated table? I.e. I want to query phrases.tsv_text. Is this even possible with the current state of the library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions