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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:07:37+00:00 2026-06-13T06:07:37+00:00

Ok so i have a pages table with all the static with this schema

  • 0

Ok so i have a pages table with all the static with this schema

create_table "pages", :force => true do |t|
  t.string   "name"
  t.text     "html"
  t.string   "url"
  t.integer  "position"
  t.boolean  "is_home"
  t.integer  "parent_id",  :default => 0, :null => false
  t.string   "nav"
end

A page can have another page as its parent. The idea is that I want the user to have complete control of the navigation and the pages. So my question what is the best way to group these pages so in the html i can loop over all the pages and their subpages

Here is what i have so far

grouped_pages = Page.where(:is_home => 0).group_by(&:nav).each do |key, group|
 group.sort_by(&:parent_id)
end

I was hoping to group them in some array and or hash combination with another layer for ever page that has a parent_id of another page

the page structure can look like this

Page1
  Page3
    Page7
  Page4
  Page5
Page2
  Page5
  Page6

Any ideas of the best way to sort and loop over to print out the uls for the html

  • 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-13T06:07:39+00:00Added an answer on June 13, 2026 at 6:07 am

    If you want to allow arbitrarily nested tree levels, the easiest solution is a recursive algorithm. For example (uses parent_id = NULL, not 0, for the page root):

    class Page < ActiveRecord::Base
      belongs_to :parent, :class_name => :Page
      has_many :children, :class_name => :Page, :foreign_key => :parent_id 
    
      def self.root
        where(:parent => nil).first
      end
    
      def tree
        [self, children.map(&:tree)]
      end
    end
    
    Page.root.tree 
    #=> returns the structure in pairs [page, children]
    

    From this you can do whatever you need. For exemple, to set a sort criterion, just add an options argument to tree and use it to scope children.

    Note that to render a tree structure you also need a recursive helper to perform the task. In fact probably you don’t need this tree method, just a recursive helper that calls children, but the idea is the same.

    As Mark points out, there are gems to implement AR trees, for example acts_as_tree.

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

Sidebar

Related Questions

I have a Pages table that stores all my view urls and this table
On my pages I have a table with rows that typically look like this:
I have a static html table on a page and i would like to
I have a big HTML table (~10K rows, 4 columns, text only) dumped from
I have web page in PHP which displays all records in a table. I
I have several pages to do with several table on each page. I've got
I'm trying to update a table from a form. I have 3 pages. The
I have a table of pages in my database, each page can have a
I have a site with multiple pages, and each page has a table that
I have the following HTML (excerpt from larger code-base) <div class=diary-event ui-corner-all title=[title]> <span

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.