· 8 years ago · Jan 24, 2018, 09:54 AM
1last = nil
2
3csv.each do |row| # rows from the table
4 base = row[6].split('/')[0] # first element
5 parent_category = Category.create!(name: base) if Category.where(name: base).first.nil? # Create a base category
6
7 row[6].split('/').each do |category| #
8 if Category.where(name: category).first.nil? # if the category does not exist
9 last = Category.create!(name: parent_category) if last == nil # create base Category
10 # if the base exists, create her child
11 child = Category.create!(name: category, ancestry: Category.where(name: base).first.id) if last != nil
12 end
13 end
14end