Skip to content

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

@lbernick

Description

@lbernick

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.

#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.

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