[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] Implementation of image enlargement function using lightbox2
Implement the product category function using ancestry ① (Preparation)
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 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] Implementation of coupon function (with automatic deletion function using batch processing)
Rails [For beginners] Implementation of comment function
Implementation of user authentication function using devise (3)
[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
[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 PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Implementation of search function
Implementation of pagination function
Implementation of Ruby on Rails login function (devise edition)
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
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
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
Rails sorting function implementation (displayed in order of number of like)
[Rails] Tag management function (using acts-as-taggable-on)
[Implementation procedure] Create a user authentication function using sorcery in Rails
Implementation of validation using regular expressions
[Ruby on Rails] Comment function implementation
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
Implementation of category selection form using JS in flea market application creation
[Android] Implementation of side-scrolling ListView using RecyclerView
[Rails] Addition of Ruby On Rails comment function
Rails Addition of easy and easy login function
[Ruby on Rails] Follow function implementation: Bidirectional
Rails Basic CRUD function implementation procedure scaffold
Implementation of tabs using TabLayout and ViewPager
[Rails] Implementation of validation that maintains uniqueness
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination