Basically, my teacher only created one model which is "categories" but then when we run the seed file:
Category.create([
{ name: "community", url: "community" }
]);
Category.create([
{ name: "activities", url: "activities", parent_id: 1 },
{ name: "artists", url: "artists", parent_id: 1 },
{ name: "childcare", url: "childcare", parent_id: 1 }
]);
As you can see there is a reference on the second Category.create.
And then on the Category model, I saw this:
class Category < ApplicationRecord
belongs_to :parent, class_name: "Category", optional: true
has_many :subcategories, class_name: "Category", foreign_key: :parent_id, dependent: :destroy
end
As you can see, my teacher did not create two different models to do the relation which looks confusing to me.
And then on the public_controller.rb file he puts:
def home
@categories = Category.where(parent_id: nil)
end
Which also a bit confusing to me.
Can anyone explain the model association made above line by line in layman's term?
Sorry I was so confuse and a newbie here.