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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:26:20+00:00 2026-06-09T08:26:20+00:00

Pardon my ignorance, I’m new to Ruby, but not programming. I’m using a gem

  • 0

Pardon my ignorance, I’m new to Ruby, but not programming. I’m using a gem called Ancestry to store hierarchical data in my application. They have a method called “descendants” that returns a nested hash of all the descending children, grandchildren, etc.. in a node, but I’m having a hard time trying to loop through each node and output all the child nodes.

Here’s what the data structure looks like

{
  #<Forum id: 16, name: "Parent 1", ancestry: "7", display_order: 1> =>{}, 
  #<Forum id: 17, name: "Parent 2", ancestry: "7", display_order: 2> =>{}, 
  #<Forum id: 13, name: "Parent 3", ancestry: "7", display_order: 3> =>
  {
    #<Forum id: 14, name: "Child 1", ancestry: "7/13", display_order: 1> =>{}, 
    #<Forum id: 15, name: "Child 2", ancestry: "7/13", display_order: 2> =>
    {
      #<Forum id: 18, name: "Grand Child", ancestry: "7/13/15", display_order: 1> =>{}, 
    }
  }
}

When I loop through that data using the code below, the parent nodes are the only ones that end up being rendered to the screen.

<% forum.descendants.arrange(:order => :display_order).map do |forum,key| %>
  <%= render :partial => 'forum', :locals => {:forum => forum} %>
<% end %>

How can I render the child nodes as well? I know they’re accessible through the “key” variable, but I don’t know how to tell when the “key” variable has data that I can render and how to output that data. Any suggestions?

  • 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-09T08:26:22+00:00Added an answer on June 9, 2026 at 8:26 am

    You can check if the key is a hash by doing key.is_a?(Hash).

    In your example, it will always evaluate to true because {} is an empty hash. You can check whether the hash is empty with key.empty?

    To iterate through the nested hash, you can do it recursively something like this

    def display hash
        hash.each do |key, val|
            # print key etc
            if val.is_a?(Hash)
                display val
            else
                # print value or whatever operation you want
            end
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pardon my ignorance, but I've never really developed Windows applications. How do you store
I am sort of new to MVC so pardon my ignorance. I am using
Pardon my ignorance on the matter but what's the point of using Query Builders?
I'm new to Ruby on Rails, so please pardon my ignorance. My issue is
Pardon the excessive amount of code, but I'm not sure if I can explain
Pardon my ignorance, but how would I begin to go about creating the following
Pardon my ignorance, I do not know much about AD (let lone querying with
I'm brand new to .net MVC3 so pardon my ignorance. I have a relatively
I'm very new to this, so pardon my ignorance. I have the following method
Overview I am doing an iOS project using the MVC architecture. Pardon my ignorance,

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.