diff --git a/Rakefile b/Rakefile index b767df0..b9a026c 100644 --- a/Rakefile +++ b/Rakefile @@ -37,8 +37,8 @@ task update_cities: :environment do end end - unless c.place_id.present? - City.all.each do |c| + City.all.each do |c| + unless c.place_id.present? location = Geocoder.search(c.address, language: 'en').first c.place_id = location.data['place_id'] c.save! diff --git a/app/models/city.rb b/app/models/city.rb index 154f004..958c945 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -66,7 +66,7 @@ class City < ActiveRecord::Base # return the city if we found it in the db already if city.present? - CityCache.create(city_id: city.id, search: str) + CityCache.cache(str, city.id) return city end @@ -119,7 +119,7 @@ class City < ActiveRecord::Base # and not an address or country # some places are not labeled 'locality', search for 'Halifax NS' for example and you will # get 'administrative_area_level_2' since Halifax is a municipality - if component['types'] == location.data['types'] && not_a_city.include?(component['types'].first) + if component['types'] == location.data['types'] && !not_a_city.include?(component['types'].first) searched_component = component['short_name'] end end @@ -135,7 +135,7 @@ class City < ActiveRecord::Base city.save! # save this to our cache - CityCache.create(city_id: city.id, search: str) + CityCache.cache(str, city.id) # and return it return city diff --git a/app/models/city_cache.rb b/app/models/city_cache.rb index d96f501..e2f8f38 100644 --- a/app/models/city_cache.rb +++ b/app/models/city_cache.rb @@ -6,4 +6,8 @@ class CityCache < ActiveRecord::Base def self.search(str) CityCache.find_by_search(str.downcase) end + + def self.cache(str, city_id) + CityCache.create(city_id: city_id, search: str.downcase) + end end