Skip to content

Instantly share code, notes, and snippets.

@mallain
Created May 19, 2010 13:07
Show Gist options
  • Save mallain/406279 to your computer and use it in GitHub Desktop.
Save mallain/406279 to your computer and use it in GitHub Desktop.
France Nord-Ouest Nantes
France Nord-Ouest Centre-Bretagne Rennes
France Nord-Ouest Centre-Bretagne Tour
France Sud-Ouest Poitou-Aquitaine Niort
France Sud-Ouest Poitou-Aquitaine Bordeaux
France Sud-Ouest Toulouse
France Sud-Est Sud Marseille
France Sud-Est Sud Nice
France Sud-Est Sud Montpellier
France Sud-Est Rhône-Alpes/Auvergne Lyon
France Sud-Est Rhône-Alpes/Auvergne Clermont-Ferrand
France Sud-Est Rhône-Alpes/Auvergne Grenoble
France Nord-Est Lille
France Nord-Est Starsbourg
France Nord-Est Amiens
# import_agencies_from_file(file_path)
#
# file_path as String
def import_agencies_from_file(file_path)
# Parse CSV File
FasterCSV.foreach(file_path) do |row|
begin
# Instanciate row variable
country_name = row[0]
division_name = row[1]
agency_name = row[2]
agencychild_name = row[3]
# Retrieve Country
country = Country.find_or_create_by_name(country_name)
# Retrieve Division
division = Division.find_or_create_by_name(division_name, :country_id => country.id) { |d| d.country = country}
# Retrieve Agency
agency = Agency.find_or_create_by_name(agency_name, :division => division) { |a| a.division = division }
# Retrieve AgencyChild if there is
if agencychild_name
Agency.find_or_create_by_name(agencychild_name, :division => division, :parent_id => agency.id){ |a| a.division = division; a.parent_id=agency.id}
end
rescue Exception => e
p e.message + " / #{row}"
end
end
end
########
# Main #
########
# Import Country/Divisions/Agencies from file
import_agencies_from_file('lib/tasks/data/agencies/agencies.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment