[RUBY] [Rails] Implementation of multi-layer category function using ancestry "seed"

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Implementation of posting function -Many-to-many category function implementationMulti-layer category function implementation (preparation)

Implementation

id name ancestry
1 business nil
2 Finance 1
3 stock 1/2
4 exchange 1/2
5 tax 1/2
6 Economy 2
7 Japanese economy 1/6
8 International economy 1/6
9 management 3
10 Business Administration 1/9
11 Strategy / Strategy 1/9
12 Company / Opening 1/9
13 marketing 4
14 Business Administration 1/13
15 Strategy / Strategy 1/13
16 Company / Opening 1/13

If you want the book category to have the above parent-child relationship, create the data as follows.

seed.rb


business = Category.create(name: 'business')
business_children_array = ['Finance', 'Economy', 'management', 'marketing']
business_grandchildren_array = [
  ['stock', 'exchange', 'tax'], #Financial child
  ['Japanese economy', 'International economy'], #Child of economy
  ['Business Administration', 'Strategy / management', 'Entrepreneurship / opening'], #Child of management
  ['Advertising', 'Sales', 'development of'] #Marketing child
]

business_children_array.each_with_index do |children, index|
  children = business.children.create(name: children)
  business_grandchildren_array[index].each do |grandchildren|
    children.children.create(name: grandchildren)
  end
end

Sequel

Multi-layer category function implementation (creation form)

Recommended Posts

[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of category function
[Rails] gem ancestry category function implementation
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails 6] Implementation of search function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Implementation of many-to-many category functions
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of new registration function in wizard format using devise
Implementation of user authentication function using devise (2)
[Rails] Category function
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] I will explain the implementation procedure of the follow function using form_with.
Implementation of Ruby on Rails login function (Session)
Implement the product category function using ancestry ① (Preparation)
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Note] Summary of rails login function using devise ①
Implementation of search function
Rails search function implementation
Implementation of pagination function
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
[Rails] Test of star evaluation function using Raty [Rspec]
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
Rails6: Input the initial data of ActionText using seed
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Rails implementation of ajax removal
Search function using [rails] ransack
Implementation of sequential search function
Implementation of like function (Ajax)
Implementation of image preview function
Implement category function using ancestory
Login function implementation with rails
Ajax bookmark function using Rails
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of automatic address input using jpostal and jp_prefecture