class PlacesIndex < ActiveRecord::Migration
def self.up
execute("CREATE INDEX place_country_id_idx ON places(country_id);")
execute("CREATE INDEX place_country_place_idx ON places(country_id,id);")
execute("CREATE INDEX place_id_idx ON places(id);")
execute("VACUUM ANALYZE places;")
end
def self.down
execute("DROP INDEX place_country_id_idx;")
execute("DROP INDEX place_country_place_idx;")
execute("DROP INDEX place_id_idx;")
end
end
class Place < ActiveRecord::Base
has_many :bookables
acts_as_world_point
def self.tsearch(query,country)
query = query.gsub(/\(.*|'/){}.split.join('&')
find_by_sql("SELECT * from places WHERE country_id = #{country}
AND tsearchable @@ to_tsquery('#{query}');")
end
def full_title
if region
r = region
else
r = Region.find(region_id).title
end
"#{title} (#{r})"
end
end
class BookablesController < ApplicationController
def index
if params[:country]
country = params[:country] ||= 79
if params[:wherelse]
@places = [Place.find(params[:wherelse])].flatten
else
@places = [Place.tsearch(params[:where],country)].flatten
end
place = @places.first.id
miles = params[:within].to_i
@town = true
end
partial:
.div
=label("where","town")
=render :partial => 'places' if @places
.div
%select{:name => "within"}
-@miles.each do |i|
%option{:value => i, :selected => selected(:within,i)}
==within #{pluralize(i,'mile')}
_places
ie the alternate best matches:
.div
%input{:type => 'hidden', :name => 'country', :value => params[:country]}
%select{:name => "wherelse"}
-@places.each do |it|
%option{:value => it.id}=it.full_title