Code Explainers

Code explainers tagged #self-referential-association

ruby
class Category < ApplicationRecord
  belongs_to :parent, class_name: "Category", optional: true
  has_many :children,
           -> { order(:position) },

Modeling a self-referential category tree in Rails

self-referential-association recursion tree-structure
Intermediate 9 steps