class AddSearchVectorToSnips < ActiveRecord::Migration[6.1]
def change
add_column :snips, :search_vector, :tsvector
add_index :snips, :search_vector, using: :gin
end
end
class Snip < ApplicationRecord
before_save :set_search_vector
private
def set_search_vector
text = [title, content].compact.join(' ')
self.search_vector = "to_tsvector('simple', #{ApplicationRecord.connection.quote(text)})"
end
end
class Snip < ApplicationRecord
scope :fts, ->(q) { where("search_vector @@ plainto_tsquery('simple', ?)", q) }
end
For Postgres search beyond trivial ILIKE, maintain a tsvector column and a GIN index. Update it via trigger or application logic. This keeps search fast and predictable even as your dataset grows.