ruby-on-railsarraysrubyancestry

ancestry: how to create categories and subcategories from the array?


my array category = ["Components for PC", "Soft", "OS"]
Number of element can come in different.
These array elements are created Category (from .csv file). In array category I need category[0] was a parent, and category[1] - child category, but the parent category[2]

Components for PC => Soft => OS

Use gem Ancestry

For two elements of the works such code (though ugly):

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

how to create categories and subcategories to any number of elements?


Solution

  • For each csv row:

    1. Get an array of category names
    2. Get the root category name and remove it from array
    3. Find or create the root category by its name

    Then, for each category name remaining in the array:

    1. Find or create the child category by its name
    2. Set the category as the parent for next categories

    csv.each do |row| # rows from the table
      category_names = row[6].split('/')
    
      root_category_name = category_names.shift
    
      parent_category = Category.find_or_create_by!(name: root_category_name) # Finds or creates the root category
    
      category_names.each do |category_name|
        parent_category = Category.find_or_create_by!(name: category_name, parent: parent_category) # Finds or creates the child category, which is the root for next categories
      end
    end