Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:56+00:00 2026-05-26T18:51:56+00:00

this is my first post in the community and a have a problema: represent

  • 0

this is my first post in the community and a have a problema: represent a tree of categories.

First solution: each category has a reference to its parent category.
The problem with this solution is, when I want a subtree of categories I have to query the database several times, one query per level (height) of the tree and I want some solution more optimized.

Chosen solution: Category (id, name, description) and CategoryxCategory (ancestor_id, descendant_id)
And so to build a sub tree I’ll need only one query like this ,

 select * from category a, categoryxcategory b.where b.ancestor_id = 1 and 
 b.descendant_id = a.id , gets all sub-categories of the category with id = 1.

I’ve done this solution in java using hibernate, but I could not do in Rails, how do I specify this in the migration and ActiveRecord?

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:51:56+00:00Added an answer on May 26, 2026 at 6:51 pm

    Can’t you just have ancestor_id and descendant_id be columns in category?

    here’s a popular gem that does trees for you https://github.com/collectiveidea/awesome_nested_set

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post, but i'm excited to join this community. I have
This is my first post on StackOverflow. This community has provided me with some
This is my first post here. I have a problem. I need to take
Well, this is my first post here and really enjoying the site. I have
This is my first post, but I have been using this site for a
Since I'm sure many people have different standard, I've made this post a community
this is my first post on stackoverlow and I couldn't find a solution to
This is my first post, so I'm just getting to know how the community
First, I must say that I have read several post about this at StackOverflow
I have a database driven community web site, where users can post entries. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.