Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- last = nil
- csv.each do |row| # rows from the table
- base = row[6].split('/')[0] # first element
- parent_category = Category.create!(name: base) if Category.where(name: base).first.nil? # Create a base category
- row[6].split('/').each do |category| #
- if Category.where(name: category).first.nil? # if the category does not exist
- last = Category.create!(name: parent_category) if last == nil # create base Category
- # if the base exists, create her child
- child = Category.create!(name: category, ancestry: Category.where(name: base).first.id) if last != nil
- end
- end
- end
Add Comment
Please, Sign In to add comment