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 8757151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:14:10+00:00 2026-06-13T14:14:10+00:00

I have a Business model that :belongs_to Category A sample hierarchy for categories: root

  • 0

I have a Business model that :belongs_to Category

A sample hierarchy for categories:

  • root
    • Restaurants
      • Sushi
      • Pizza
      • Chinese
      • French
      • Italian

I am using acts_as_tree for Category hierarchy.

How can I find all Businesses under the Restaurants category?

  • 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-06-13T14:14:11+00:00Added an answer on June 13, 2026 at 2:14 pm

    If you want to get all descendants of a node in a tree-like hierarchy you have two options:

    1. Use the classic acts_as_tree, preload the selected category, start a cascade of queries to retrieve all the children, the grandchildren etc, until you get only leaves (nodes without further chidren). This approach is as good as it sounds.

    2. Use a more advanced tree representation, like nested set or closure tree. Under these representation you can get all descendants of a specific node with just one single query.

    Then you get the collected categories and query in Businesses:

    Business.where(:category => categories)
    

    (technical explanation: nested set)

    Under a nested set representation, each node has two indices, assigned in the following way: imagine that each node is a house with two windows, East and West, and the tree is like a bifurcating road where all children are more or less to the north of their parent. So you start to the east of the root house, and put a sequential number on the windows you meet. You never cross any road, you are only allowed to go around houses which has no further road to the north. At the end you’ll get again to the root house, and put a number on the west window.

    The assigned numbers will have the following properties:

    • All east windows have even numbers, all west windows have odd numbers (provided you started with 0)
    • The delta in each house (difference between west and east number) is always 1 + the number of descendant houses
    • All descendant houses of any house H will have their east and west numbers strictly included between H’s own numbers
    • The converse is true too, all houses whose numbers are strictly comprised between H’s numbers are actually descendants of H
    • If two houses are not ancestors one of the other (so one’s nombers are comprised between the other’s) then they are completely separated, that is both numbers of one house are strictly less than both numbers of the other.

    So, while inserting a new element in the tree is costly (it needs to update a number of indices), retrieving the whole descendance (all children, grandchildren, …) is quite easy, just take the nodes whose “east” and “west” numbers are between your chosen category’s east and west. You can actully do slightly better, but it doesn’t matter here.

    A library like https://github.com/collectiveidea/awesome_nested_set will manage all this for you, and you only call

    categories = @category.self_and_descendants.to_a
    

    (technical explanation: closure tree)

    This approach need an accessory table, where you store the transitive closure of the child->parent relationship (see http://en.wikipedia.org/wiki/Reflexive_transitive_closure#P_closures_of_binary_relations )

    The table will contain all pairs ancestor-descendant, so you can join in smart ways with that table to get almost any slicing of the hierarchy.

    Again, a library such as https://github.com/mceachen/closure_tree will do the hard work for you, and you will be able to do

    categories = @category.self_and_descendants.to_a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my business model I have Entity class (IPoint interface ) that has a
I have a DB model that uses several tables to hold business logic data;
I have Business model that has an active field that defaults to False. This
I have a Model that looks something like this: class Business(models.Model): name = models.CharField('business
I have an application in CakePHP that lists businesses. I have a business model/controller,
I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email
I have a model called Business , and I want to save Business.all into
We have a business requirement that some users be automatically logged out of our
I have a business object that compiles into a DLL that handles all calculations
I have a business model called Customer which has many required properties (via DataAnnotations)

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.