From 196bc049014f78bf0c0ffb253471360f13a280ca Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 29 Oct 2015 14:24:24 +0100 Subject: [PATCH] adds pg_search to debates --- app/models/debate.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index 0b64b7b1e..17560e246 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -5,6 +5,7 @@ class Debate < ActiveRecord::Base include Conflictable include Measurable include Sanitizable + include PgSearch apply_simple_captcha acts_as_votable @@ -36,6 +37,22 @@ class Debate < ActiveRecord::Base # Ahoy setup visitable # Ahoy will automatically assign visit_id on create + pg_search_scope :pg_search, { + against: { + title: 'A', + description: 'B' + }, + associated_against: { + tags: :name + }, + using: { + tsearch: { dictionary: "spanish" }, + trigram: { threshold: 0.1 }, + }, + ranked_by: '(:tsearch + debates.cached_votes_up)', + order_within_rank: "debates.created_at DESC" + } + def description super.try :html_safe end @@ -102,12 +119,7 @@ class Debate < ActiveRecord::Base end def self.search(terms) - return none unless terms.present? - - debate_ids = where("debates.title ILIKE ? OR debates.description ILIKE ?", - "%#{terms}%", "%#{terms}%").pluck(:id) - tag_ids = tagged_with(terms, wild: true, any: true).pluck(:id) - where(id: [debate_ids, tag_ids].flatten.compact) + self.pg_search(terms) end def after_hide