Copy
Copy
migrate.rb
1
2
3
4
Category
Sub Category
Sub Category
Sub Category
1
2
3
4
5
6
7
8
9
10
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.references :parent
t.timestamps
end
end
end
Copy
model.rb
1
2
3
4
5
class Category < ApplicationRecord
has_many :categories, class_name: 'Category', foreign_key: 'parent_id',
dependent: :destroy
belongs_to :parent, class_name: 'Category', optional: true
end
c.parent
c.categories