Skip to content

Instantly share code, notes, and snippets.

@developish
Created January 7, 2010 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save developish/271279 to your computer and use it in GitHub Desktop.
Save developish/271279 to your computer and use it in GitHub Desktop.
How to use searchlogic and geokit together
@zipcode = Zipcode.find_by_name("85023")
# Passing in a hash of search parameters is more convenient for my purposes,
# but searchlogic allows the second, chained method of searching also works.
@locations = Location.by_location(:origin => @zipcode, :within => 10).search(:name_like => "searchlogic", :city_is => "Phoenix")
@locations = Location.by_location(:origin => @zipcode, :within => 10).name_like("searchlogic").city_is("Phoenix")
# create_table "locations", :force => true do |t|
# t.string "name"
# t.decimal "lat", :precision => 15, :scale => 10
# t.decimal "lng", :precision => 15, :scale => 10
# t.string "address1"
# t.string "address2"
# t.string "city"
# t.string "state"
# t.string "zip"
# t.datetime "created_at"
# t.datetime "updated_at"
# end
class Location < ActiveRecord::Base
acts_as_mappable
# For more info see:
# http://www.napcsweb.com/blog/2009/08/11/geokit-and-named_scope/
named_scope :map_conditions, lambda { |*args| {:conditions => args} }
def self.by_location(options ={})
scope = self.scoped({})
scope = scope.map_conditions "#{distance_sql(options[:origin])} <= #{options[:within]}"
scope
end
end
# create_table "zipcodes", :force => true do |t|
# t.string "name"
# t.decimal "lat", :precision => 15, :scale => 10
# t.decimal "lng", :precision => 15, :scale => 10
# t.datetime "created_at"
# t.datetime "updated_at"
# end
class Zipcode < ActiveRecord::Base
acts_as_mappable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment