Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lbernick opened this issue Oct 4, 2024 · 0 comments
Open

Comments

@lbernick
Copy link

lbernick commented Oct 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant