Created
May 16, 2016 23:29
-
-
Save leandronsp/7bd22885d43736f2c41497aa34ad68f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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