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

  • Home
  • SEARCH
  • 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 7191285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:41:49+00:00 2026-05-28T19:41:49+00:00

I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter

  • 0

I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter in this case I know that it doesnt match the RoR naming convention but I have good reasons for this… Model looks like this:

has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'
belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
....
...
..
scope :top_categories, where("parent_id IS NULL")

I had a similar problem with the view here and this works well but now i need a dropdown with all the categories displayed in its hierarchie. This is really messing with me i simply dont get it! I want the top categories including the children and the children should include her children,… How can I display this in an array?

I need these data for a collection_select which should display the whole navigation but not on one area I want to display it like this:

Top-Category1
---Sub-Category1
------Sub-Category1-1
------Sub-Category1-2
---Sub-Category2
Top-Category2
---Sub-Category1

Can anyone help me?

// Solved it like this:

  def self.recursive_categories(categories, prefix='')
            c = []
            categories.collect do |cat|
                    current = Struct::Category.new
                    current.id = cat.id
                    current.name =  "#{prefix}#{cat.name}"
                    c << current
                    if cat.children
                           c += recursive_categories( cat.children, prefix + '--' )
                    end
            end
            c
  end

I defined Struct::Category in ApplicationHelper which is included in ApplicationController. Then I used this to display it in form:

<%= f.collection_select :category_id, Category.recursive_categories(Category.top_categories), :id, :name %>
  • 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-28T19:41:51+00:00Added an answer on May 28, 2026 at 7:41 pm

    Fairly simple recursion to get the choices array ready for select:

    # in a helper
    def recursive_options categories, prefix=''
      c = []
      categories.collect do |cat|
        c << [ prefix + cat.name, cat.id ]
        if cat.categories
          c += recursive_options( cat.categories, prefix + '--' )
        end
      end
      c
    end
    
    # then in your view somewhere
    <%= f.select :category_id, recursive_options( Category.top_categories )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have the following complex model: Category - has -> List which has ->
I have a model question which has a column called category. I have an
I have a model for a task which has a name, description, the process
I have a model Category which has a FK reference to itself. How can
I have a Category Model which has a ParentID field of int? datatype. I'm
In my code I have a Category model which has many subcategories, and the
I have model Foo which has field bar. The bar field should be unique,
I have hibernate database with three model: Article - which has is part of
Right now my products model has a string column of category, with the form

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.