Created
January 7, 2010 15:09
-
-
Save developish/271279 to your computer and use it in GitHub Desktop.
How to use searchlogic and geokit together
This file contains hidden or 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
@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") |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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