Implement category function using ancestory

Introduction

When I went to a certain programming school and created a flea market app as a team, I implemented a multi-layered structure of categories, so I will introduce it! !!

Completion drawing

Image from Gyazo

Then I would like to introduce it.

1. Creating a model

First, I want to link products and categories, so I will create models for "category" and "item".

$ rails g model category 

$ rails g model item 

2. Introduction of ance story

Introduce ancestory to Gemfile.

gem 'ancestry'

Execute the following command in the terminal

$ bundle install

$ rails g migration add_ancestry_to_category ancestry:string:index

$ rails db:migrate

3. Association

By using ancestory, the many-to-many relationship becomes a one-to-many relationship and the DB design becomes simple.

Describe has_ancestory in category.rb

category.rb


class Category < ApplicationRecord
 has_many :items
 has_ancestry  
end

item.rb


class Item < ApplicationRecord
 belongs_to :category
end

3. Introduce data

We will introduce category records into seeds.rb.

seeds.rb


lady = Category.create(name: "Women")
lady_1 = lady.children.create(name: "tops")
lady_1.children.create([{name: "T-shirt/Cut and sew(Short sleeves/Sleeveless)"},{name: "T-shirt/Cut and sew(Seven minutes/Long sleeves)"},{name: "shirt/blouse(Short sleeves/Sleeveless)"},{name: "shirt/blouse(Seven minutes/Long sleeves)"},{name: "ポロshirt"},{name: "camisole"},{name: "Tank top"},{name: "Halter neck"},{name: "knit/sweater"},{name: "Tunic"},{name: "cardigan/Bolero"},{name: "ensemble"},{name: "Best/Gillet"},{name: "Parker"},{name: "trainer/sweat"},{name: "Bare top/Tube top"},{name: "Jersey"},{name: "Other"}])

If you include ancestry, you can treat it as a child element of the immediately preceding variable by writing .children.

The parent category, Ladies, is registered with the following description.

seeds.rb


lady = Category.create(name: "Women")

Next, the child category Tops is registered with the following description.

seeds.rb


lady_1 = lady.children.create(name: "tops")

Then, the grandchild category group is registered with the following description.

seeds.rb


lady_1.children.create([{name: "T-shirt/Cut and sew(Short sleeves/Sleeveless)"},{name: "T-shirt/Cut and sew(Seven minutes/Long sleeves)"},{name: "shirt/blouse(Short sleeves/Sleeveless)"},{name: "shirt/blouse(Seven minutes/Long sleeves)"},{name: "ポロshirt"},{name: "camisole"},{name: "Tank top"},{name: "Halter neck"},{name: "knit/sweater"},{name: "Tunic"},{name: "cardigan/Bolero"},{name: "ensemble"},{name: "Best/Gillet"},{name: "Parker"},{name: "trainer/sweat"},{name: "Bare top/Tube top"},{name: "Jersey"},{name: "Other"}])

After describing the record in seeds.rb, execute the following command in the terminal to reflect it in the DB.

$ rails db:seed

It is registered in DB like this.

Image from Gyazo

Thank you for watching till the end! !!

I hope you found this article helpful: pray_tone2:

Recommended Posts

Implement category function using ancestory
Implement the product category function using ancestry ① (Preparation)
Implement user edit / update function without using devise
Implement the star five function using the for statement
Implement partial match search function without using Ransuck
Implement star rating function using Raty in Rails6
Flow to implement image posting function using ActiveStorage
Implement ProgressBar using library
[Rails] Implement search function
Continued ・ Flow to implement image posting function using ActiveStorage
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails learning How to implement search function using ActiveModel
Try to implement tagging function using rails and js
Implement application function in Rails
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implement User search function
Search function using [rails] ransack
Implement follow function in Rails
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of category function
[Rails] Implement event end function (logical deletion) using paranoia (gem)
Implementation of category pull-down function
Ajax bookmark function using Rails
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
Implement search function with form_with
[Swift] Implement UITableView using xib
[Rails] Implement community membership application / approval function using many-to-many associations
[Swift] How to implement the Twitter login function using Firebase UI ①
[Swift] How to implement the Twitter login function using Firebase UI ②
[Rails] Tag management function (using acts-as-taggable-on)
Implement declarative retry processing using Spring Retry
Implement user management functionality using Devise
Try to implement iOS14 Widget function
Implement simple login function in Rails
Implement tagging function in form object
[Swift 5] Implement UI for review function
Implement search functionality using LIKE clauses
[Java] Try to implement using generics
[Rails] gem ancestry category function implementation
Implement CSV download function in Rails
Implement pagination using SpringData Pageable + Thymeleaf