Skip to content

Instantly share code, notes, and snippets.

@ExByt3s
Created February 9, 2015 22:38
Show Gist options
  • Save ExByt3s/bedb6d81259505a7e87f to your computer and use it in GitHub Desktop.
Save ExByt3s/bedb6d81259505a7e87f to your computer and use it in GitHub Desktop.
bulk category T___T
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
#
Category.delete_all
Category.create(:name=>"root", :id => 0, :category_id => -1)
open("db/categories.txt") do |categories|
categories.read.each_line do |category|
next if category[0]=='#' #skip comments
cat, *subcats = category.chomp.split(">")
cat=cat.strip #remove trailing and leading whitespaces
#check if category exists, and create if it doesn't
db_entry= Category.roots.subcategories.where(:name=>cat).first
if db_entry.blank? then
db_entry=Category.roots.subcategories.create(:name=>cat)
end
#Print some info to show progress
print cat
print " > "
#add subcategories
subcats.each do |subcat|
subcat=subcat.strip
db_subentry = db_entry.subcategories.where(:name=>subcat).first
if db_subentry.blank? then
db_subentry = db_entry.subcategories.create(:name=>subcat)
end
db_entry=db_subentry
#Print some info to show progress
print subcat
print " > "
end
print "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment