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

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 implementation

Implementation

1. Introduce Gem

Gemfile


#Postscript
gem 'ancestry'

Terminal


$ bundle

2. Add column

Since the amount of data will be quite large, I will put ʻindex`.

Terminal


$ rails g migration AddAncestryToCategory ancestry:string:index

Terminal


$ rails db:migrate

schema.rb


create_table "categories", force: :cascade do |t|
  t.string "name"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "ancestry"
  t.index ["ancestry"], name: "index_categories_on_ancestry"
end

3. Edit the model

category.rb


#Postscript
has_ancestry

has_ancestry ➡︎ You will be able to use ancestry.

Method list

Method name Return value
parent Get parent record
parent_id Get the ID of the parent record
root Get the root of a record
root_id Get the root ID of a record
root?is_root? Returns true if the record is root
ancestors Returns the ancestor of a record, starting at the root and ending at the parent
ancestors? Returns true if the record has an ancestor (not the root node)
ancestor_ids Returns the ID of the record's ancestors
path Returns the path of the record, starting at root and ending at self
path_ids Returns a list of path IDs that start with the root ID and end with your own ID
children Get child records
child_ids Get the ID of the child record
has_parent?ancestors? Returns true if the record has a parent
has_children?children? Returns true if the record has children
is_childless?childless? Returns true if the record has no children
siblings Returns sibling records (records in the same hierarchy)
sibling_ids Returns the ID of sibling records (records in the same hierarchy)
has_siblings?siblings? Returns true if the record parent has multiple children
is_only_child?only_child? Returns true if the record is the only child of the parent
descendants Child record, grandchild record, great-grandchild record...return it
descendant_ids Child record, grandchild record, great-grandchild record...Returns the ID of
indirects Returns less than or equal to grandchild record
indirect_ids Returns the ID below the grandchild record
subtree Returns a model of offspring and self
subtree_ids Returns a list of record subtree IDs
depth Returns the depth of the node
parent_of?(node) This record(node)To be a parent of
root_of?(node) This record(node)To the root of
ancestor_of?(node) (node)Ancestors contain this record
child_of?(node) (node)Is the parent of the record
descendant_of?(node) (node)Is one of the ancestors of this record
indirect_of?(node) (node)Is one of the ancestors of this record, but not the parent

Hierarchical structure

** Parent: Business ** ** Child: Economy ** ** Son: Japanese economy, international economy **

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

python


business = Category.create(name: 'business')

business_economy = business.children.create(name: 'Economy')

business_economy.children.create([{ name: 'Japanese economy' }, { name: 'International economy' }])

Column structure

id name ancestry
1 business nil
2 Economy 1(Parent id)
3 Japanese economy 1/2(Parent id/Child id)
4 International economy 1/2(Parent id/Child id)

Sequel

Multi-layer category function implementation (seed edition)

Recommended Posts

[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[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
Implement the product category function using ancestry ① (Preparation)
[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] Implementation of image preview 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
[Rails] Category function
Implementation of user authentication function using devise (1)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
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)
[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]
[Ruby on Rails] Implementation of tagging function/tag filtering function
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Rails implementation of ajax removal
Rails fuzzy search function implementation
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