Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PostgreSQL full-text search support for ActiveRecord
module PostgresqlFullTextSearch
extend ActiveSupport::Concern
module ClassMethods
def tsearch(column_names, terms)
raise(ArgumentError) if terms.nil?
return self.scoped if terms == ""
vectors = Array(column_names).map do |column_name|
"to_tsvector(coalesce(#{column_name}, ''))"
end
conditions = vectors.join(' || ') + " @@ to_tsquery(?)"
tokens = terms.split.map do |word|
connection.quote(word) + ":*"
end
query = tokens.join(" & ")
where(conditions, query)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment