Skip to content

Instantly share code, notes, and snippets.

@leandronsp
Created May 16, 2016 23:29
Show Gist options
  • Save leandronsp/7bd22885d43736f2c41497aa34ad68f1 to your computer and use it in GitHub Desktop.
Save leandronsp/7bd22885d43736f2c41497aa34ad68f1 to your computer and use it in GitHub Desktop.
class City < ActiveRecord::Base
include PgSearch
belongs_to :region
belongs_to :country
has_many :city_aliases
##
# Search against a single field is not so expensive,
##
pg_search_scope :cheap_search,
against: :name,
ignoring: :accents,
ranked_by: ":tsearch * log(GREATEST(CAST(population AS integer), 3))",
order_with_rank: 'pg_search_rank DESC'
##
# Search against associated fields will produce join queries
##
pg_search_scope :expensive_search,
against: { name: 'A' },
associated_against: {
city_aliases: { name: 'B' },
region: { name: 'C' },
country: { name: 'D' }
},
ignoring: accents,
ranked_by: ":tsearch * log(GREATEST(CAST(population AS integer), 3))",
order_with_rank: 'pg_search_rank DESC'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment