Last active
April 2, 2016 00:35
-
-
Save dodops/92344910dad0f8d22a25 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
# Creditos: https://github.com/celsodantas/br_populate | |
# Models: City - State | |
require 'net/http' | |
require 'net/https' # for ruby 1.8.7 | |
require 'json' | |
def states | |
http = Net::HTTP.new('raw.githubusercontent.com', 443); http.use_ssl = true | |
JSON.parse http.get('/zigotto/br_populate/master/states.json').body | |
end | |
def capital?(city, state) | |
city == state["capital"] | |
end | |
ActiveRecord::Base.transaction do | |
states.each do |state| | |
state_obj = State.find_or_create_by(acronym: state["acronym"], name: state["name"]) | |
state["cities"].map { |city| City.find_or_create_by(name: city, state: state_obj, capital: capital?(city, state)) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment