2

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?

Community
  • 1
  • 1
sites
  • 21,417
  • 17
  • 87
  • 146

2 Answers2

1

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
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
0

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.

earth2jason
  • 715
  • 1
  • 9
  • 20
  • scope defines a class method, `against` receives column names which are instance methods, an option could be to cache skus in a new column, and updating them when variant changes. But scope is not an option from what I see. – sites Jul 01 '14 at 02:33