Skip to content

Instantly share code, notes, and snippets.

@scttymn
Created September 27, 2012 23:26
Show Gist options
  • Save scttymn/3797071 to your computer and use it in GitHub Desktop.
Save scttymn/3797071 to your computer and use it in GitHub Desktop.
module PgSearchable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def searchable_on(*args)
@@search_columns = args
end
def pg_search(terms = nil)
return self.scoped if terms.blank?
document = @@search_columns.join(" || ' ' || ")
query = terms.split.map{|term| "#{term}:*"}.join(" & ")
where("to_tsvector(#{document}) @@ to_tsquery(?)", query)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment