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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:16:08+00:00 2026-05-30T10:16:08+00:00

I am trying to create my app using the Nested Set Gem here I

  • 0

I am trying to create my app using the Nested Set Gem here
I am trying to create a simple product menu for my app, so I would have something like this

+Category
–Category
—-Product
—-Product
–Category
—-Product
—-Product

My problem with the nested Set gem is that I don’t want the depth two be more then two category’s deep. The nested set gem by default allows something like this

+Category
–Category
—Category
—-Category
—–Product

But for our CSS Styling purposes, we don’t want to deal with that much depth, and the customer doesn’t need that much depth to it.

So i’m wondering if I even need the nested set in the first place, or will a standard has_many belongs_to work in this scenario? I am using Nested Set from the responses to this question here.

I am personally under the opinion that I may just use a standard has_many and belongs to, unless someone can inform me an advantage to using Nested Set in my case. If I do need it, how do I limit the customer from only selecting the correct depth to add a node to?

EDIT: Solved, Thanks.
My new problem is I am getting the error

undefined method `self_and_descendants’ for []:ActiveRecord::Relation
Extracted source (around line #9):

6: </p>
7: <p>
8:   <%= f.label(:parent_id) %>
9:   <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i,     level| "#{'-' * level} #{i.name}" } %>
10: </p>
11: <p>
12:   <% # f.label(:position) %>

views/categories/_form.html.erb

<%= form_for(@category) do |f| %>
<p>
  <%= f.label(:name) %>
  <%= f.text_field :name %>
</p>
<p>
  <%= f.label(:parent_id) %>
    <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i, level| "#{'-' * level} #{i.name}" } %>
</p>
<p>
  <% # f.label(:position) %>
  <% # f.select :position, 1..category_count %>
</p>
<p>
  <%= f.submit("Submit") %>
</p>
<% end %>

models/category.rb

class Category < ActiveRecord::Base
  acts_as_nested_set
  acts_as_list :scope => :parent_id
  has_many :products
  scope :position, order("position asc")
  scope :categories, where("parent_id IS NULL")
  scope :subcategories, where("parent_id IS NOT NULL")
end

routes.rb (Shortened for space)

Locksmithing::Application.routes.draw do
resources :categories do
  collection { post :sort }
  resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory]
  end
end
  • 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-30T10:16:09+00:00Added an answer on May 30, 2026 at 10:16 am

    You could definitely continue using nested_set in this kind of context.

    I have a hierarchical setup in the app I’m currently working on and while I allow infinite nesting, I only want the nav to show top 2 levels.

    I have a named scope to let me limit what I’m looking at:

    scope :with_depth_below, lambda { |level|
      where(self.arel_table[:depth].lt(level))
    }
    

    Such that (using Category from your example) you could use

    Category.with_depth_below(4).arrange
    

    You can pass that scope into any of nested_set’s scopes – or indeed the View Helper you’re probably aiming to use for limiting how many levels it can go.

    To genuinely limit it, I’d also just place a validation constraint… before_save, checking that the depth is less than your max depth. Make sense?

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

Sidebar

Related Questions

I'm new here. I have been trying to create a video capture app using
Im trying to create a simple pan and zoom app using silverlight 4, but
Hi im trying to create a simple android app using the Eclipse IDE, all
I'm trying to create a instagram app using the php library which is here(its
I am trying to create a simple web app using Python on GAE. The
i am trying to create a simple android app using eclipse and android sdk
I am trying to develop a iphone app by using cocos2d. I create a
I am trying to create a simple app that displays a list of items
I'm trying to create a WinForms app that takes a screenshot on a set
Trying to create my first iPhone app that would play back audio. When I

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.