Created
September 3, 2012 16:59
-
-
Save safarista/3610846 to your computer and use it in GitHub Desktop.
Seeding a database using CSV module. A complete noob style.
This file contains 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
# encoding: utf-8 | |
# | |
# The job here was to seed the database with | |
# A mega list of business directory | |
# the directories have subdirectories | |
# The result is found on http://enterprisesthelena.com/directories | |
# What do you think? Is there a better way to do it? | |
require 'ap' | |
require 'csv' | |
entries = CSV.read('businessDirectorylatest.csv', encoding: 'utf-8')[1..-1] # Is the [1..-1] right? The encoded to force french to obey me | |
directories = entries.map { |l| l[0] }.uniq | |
sub_directories = entries.map { |l| [l[0], l[1]] }.uniq | |
businesses = entries.map { |l| [l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7], l[8]] } | |
# create the directories | |
directories.each { |cat| Directory.create( name: cat) } | |
# find the directory and create its sub_directories | |
sub_directories.each { |parent, sc| Directory.find_by_name(parent).sub_directories.create(name: sc) } | |
# for each sub_directory | |
businesses.each do |bz| | |
# find the sub_directory and create its related business/org | |
SubDirectoryory.find_by_name(bz[1]).organisation.create( | |
org_contact: bz[2], | |
org_name: bz[3], | |
address: bz[4], | |
telephone: bz[5], | |
fax: bz[6], | |
email: bz[7], | |
website: bz[8] | |
) | |
end | |
# print them out | |
puts businesses |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment