I recently added product categories to my website. I wanted to create a few top-level categories with some more specific sub-categories. I decided that a parent/child relationship would be suitable for this. First, I had to add a foreign key to the model for parent_id. I added a belongs_to relationship called parent and a has_many relationship called children as shown below.
I also added a recusrive method called get_ancestors which simplifies printing the category hierarchy.
Finally, I added a partial which prints the category hierarchy.