Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save augcesar/98bb2401ae3c6d60f65c335168592df2 to your computer and use it in GitHub Desktop.
Save augcesar/98bb2401ae3c6d60f65c335168592df2 to your computer and use it in GitHub Desktop.
estados = JSON.parse open('https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json').read
estados['estados'].select{|x| x['sigla'] != 'CE' }.each do |aux|
estado = Estado.find_or_create_by(sigla: aux['sigla'], descricao: aux['nome'].mb_chars.upcase.to_s
)
aux['cidades'].each do |cidade|
Municipio.find_or_create_by(estado:estado, descricao:cidade.mb_chars.upcase.to_s
)
end
end
estados = JSON.parse open('https://raw.githubusercontent.com/clubedaentrega/municipios-ibge/master/dados.json').read
estados.select{|x,y| x != 'CE' }.each do |aux|
estado = Estado.find_by!(sigla: aux.first)
# Cidades
aux.second.each do |cidade, codigo|
cidade = I18n.transliterate(cidade.mb_chars.upcase.to_s).gsub(' ', '')
Municipio.where(estado: estado).where("replace(descricao, ' ', '') = '#{cidade}'")&.take&.update(codigo: codigo)
end
end
@augcesar
Copy link
Author

Existe um select retirando o Estado CE, pois já importei :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment