Created
July 31, 2014 18:52
-
-
Save enzoz/8f652214cfd9f8ad0472 to your computer and use it in GitHub Desktop.
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
| class StatesAndCitiesImporter | |
| def self.import | |
| new.run | |
| end | |
| def initialize | |
| seed_file = File.open("#{Rails.root}/vendor/seeds/states_and_cities.json", "rb") | |
| @states_and_cities = ActiveSupport::JSON.decode(seed_file.read) | |
| end | |
| def run | |
| @states_and_cities.each do |state_attributes| | |
| cities_name = state_attributes.delete("cities") | |
| state = State.create!(state_attributes) | |
| cities_name.each do |city_name| | |
| City.create! name: city_name, state: state, capital: (city_name == state.capital) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment