Guest User

Untitled

a guest
Jan 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. last = nil
  2.  
  3. csv.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
  14. end
Add Comment
Please, Sign In to add comment