sites
sites

Reputation: 21795

pg_search with associated model field

I read this.

I tried it:

class Product < ActiveRecord::Base
  has_many :variants
  def skus; variants.map(&:sku).join(' ') end

  multisearchable :against => [:name, :slug, :skus]
end

but I am getting:

PgSearch::Multisearch.rebuild(Product)
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column products.skus does not exist

What am I doing wrong? Could I take another alternative?

Upvotes: 2

Views: 1156

Answers (2)

Unixmonkey
Unixmonkey

Reputation: 18784

I've bumped into this recently also. The multisearch reindex method assumes that all things that are mutisearchable against are database columns, and not methods.

There is an issue filed against this behavior here: https://github.com/Casecommons/pg_search/issues/157

The simplest way to workaround this is to declare your multisearchable call with a proc that must be evaluated like so:

multisearchable :against => [:name, :slug, :skus],
                :if => proc{ true }

You can see how that changes the rebuild strategy here.

You could also redefine the rebuild_pg_search_documents method on your model as described in the README:

def self.rebuild_pg_search_documents
  find_each { |record| record.update_pg_search_document }
end

Upvotes: 1

earth2jason
earth2jason

Reputation: 717

Off the top of my head, skus needs to be scope, not a method.

I know this is short answer, but not enough reps to just add as a comment.

Upvotes: 0

Related Questions