Skip to content

Instantly share code, notes, and snippets.

@enzoz
Created July 31, 2014 18:52
Show Gist options
  • Select an option

  • Save enzoz/8f652214cfd9f8ad0472 to your computer and use it in GitHub Desktop.

Select an option

Save enzoz/8f652214cfd9f8ad0472 to your computer and use it in GitHub Desktop.
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