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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:39:00+00:00 2026-06-14T13:39:00+00:00

I have an array of @pages below #<Page id: 1, url: /location1, name: Information,

  • 0

I have an array of @pages below

#<Page id: 1, url: "/location1", name: "Information", sort_order: 2, parent_id: nil>
#<Page id: 2, url: "/location2", name: "Information 2", sort_order: 2, parent_id: 4>
#<Page id: 3, url: "/location3", name: "Information 3", sort_order: 1, parent_id: >
#<Page id: 4, url: "/location4", name: "Information 4", sort_order: 1, parent_id: nil>
#<Page id: 5, url: "/location5", name: "Information 5", sort_order: 1, parent_id: 2>
#<Page id: 6, url: "/location6", name: "Information 6", sort_order: 3, parent_id: nil>

And i am trying to build a nav with these pages…Note this is just an example I really have 70 pages similar to this

I want the final outcome to look like this

<ul>
  <li><a href="/location4">Information 4</a>
    <ul>
      <li><a href="/location3">Information 3</a></li>
      <li><a href="/location2">Information 2</a>
        <ul><li><a href="/location5">Information 5</a></li></ul>
      </li>
    </ul>
  </li>
  <li><a href="/location1">Information 1</a></li>
  <li><a href="/location6">Information 6</a></li>
</ul>

So the parent_id will signal if the li has another child ul and li and the sort order is the ordering of the chil li’s

I cant seem to wrap my brain around how i need to loop over @pages efficiently …any ideas..

  • 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-14T13:39:02+00:00Added an answer on June 14, 2026 at 1:39 pm

    Start with only top level Pages, i.e. Pages where parent_id == nil, ordered by sort_order

    Define a children method that gets you all Pages where parent_id == self.id, ordered by sort_order

    Then you should be able to do something like this:

    def build_navigation(pages, html = nil)
      return "" if pages.length == 0
    
      navigation_html = html || ""
      navigation_html << "<ul>"
    
      pages.each do |page|
        navigation_html << li_tag(page)
        navigation_html << build_navigation(page.children, navigation_html)
      end
    
      navigation_html << "</ul>"
    end
    
    def li_tag(page)
      "<li><a href='#{page.name}'>#{page.name}</a></li>"
    end
    
    build_navigation(parent_pages).html_safe
    

    Update:
    Slightly adapted so it works when you only want to do ONE query:

    def all_pages
      # get all the pages from the DB
    end
    
    def parent_pages(pages)
      parents = pages.reject { |page| page.parent_id.nil? }
      sort(parents)
    end
    
    def children(parent, pages)
      children = pages.map { |page| page.parent_id == parent.id }
      sort(children)
    end
    
    def sort(pages)
      pages.sort { |a, b| a.sort_order <=> b.sort_order }      
    end
    
    def build_navigation(pages, html = nil)
      return "" if pages.length == 0
    
      navigation_html = html || ""
      navigation_html << "<ul>"
    
      pages.each do |page|
        navigation_html << li_tag(page)
        navigation_html << build_navigation(children(page, all_pages), navigation_html)
      end
    
      navigation_html << "</ul>"
    end
    
    def li_tag(page)
      "<li><a href='#{page.name}'>#{page.name}</a></li>"
    end
    
    build_navigation(parent_pages(all_pages)).html_safe
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array like below the page. I want to find array number which
I have an array that I want on multiple pages, so I made it
So I have a multidimensional array, containing 36 arrays. I want for a page
I have a page that creates an associative array, then passes it as a
I have a few html image tags that fill an array. On the page
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have a jquery script as below: $.ajax({ type: GET, url: http://www.site.com/v4/ajax/get_song_info.php, data: ({id
I have a script, see below: Index page: jQuery script <script type=text/javascript> $(document).ready(function(){ $(#loadmorebutton).click(function
I am trying to show information from database without reloading the page. I have
I have a database table like below: id | igroup | title | url

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.