Fix: Ambiguous column error in card search with multiple terms
Problem
When searching cards with multiple terms, SQLite raised an ambiguous column error:
SQLite3::SQLException: ambiguous column name: search_records_fts
This occurred because each term in the search was calling .mentioning() separately in a reduce loop:
result = terms.reduce(result) do |result, term|
result.mentioning(term, user: creator)
end
Each .mentioning() call added another INNER JOIN to the search_records_fts table, creating duplicate joins with the same table name, which SQLite couldn't disambiguate.
Solution
Combined all search terms into a single FTS query using the AND operator before calling .mentioning() once:
result = result.mentioning(combined_fts_query, user: creator) if terms.present?
This ensures only one join to search_records_fts is created, regardless of the number of search terms.
Also added FTS sanitization by escaping double quotes to prevent query syntax errors when users search for terms containing special characters like O"Reilly or user's input.