・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Implementation of posting function -Many-to-many category function implementation
Gemfile
#Postscript
gem 'ancestry'
Terminal
$ bundle
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
category.rb
#Postscript
has_ancestry
has_ancestry
➡︎ You will be able to use ancestry.
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 |
** 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' }])
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) |
Multi-layer category function implementation (seed edition)
Recommended Posts