Skip to content

Instantly share code, notes, and snippets.

@dodops
Last active April 2, 2016 00:35
Show Gist options
  • Save dodops/92344910dad0f8d22a25 to your computer and use it in GitHub Desktop.
Save dodops/92344910dad0f8d22a25 to your computer and use it in GitHub Desktop.
# 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