Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
http://www.casebook.net
MIT License
1.35k stars 370 forks source link

pg_search_documents not updated for a model association with `touch: true` #543

Open lbernick opened 1 month ago

lbernick commented 1 month ago

I have the following models:

class Part
  multisearchable against: %i[name description part_number_formatted]
  has_one :part_number
  delegate :formatted, to: :part_number, prefix: true
class PartNumber
  belongs_to :part, touch: true

This seems to be the approach recommended by this stackoverflow post for searching attributes across associations.

https://github.com/Casecommons/pg_search/issues/157 is similar, but I'm observing a different problem: Since update_pg_search_documents is called only after_save, the pg_search_document content is not updated when the part number is updated, even with touch: true.

My workaround was:

class Part
   after_touch :update_pg_search_document
class PartNumber
  after_destroy :reset_part_association
  after_create :reset_part_association
  def reset_part_association
    part&.association(:part_number)&.reset
  end

The changes to PartNumber were necessary so that when part.part_number.destroy is called, part.part_number is reloaded and the pg_search_document content stops including the formatted part number.