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.
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.
I have the following models:
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 onlyafter_save
, the pg_search_document content is not updated when the part number is updated, even withtouch: true
.My workaround was:
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.