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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:41:45+00:00 2026-05-30T23:41:45+00:00

I am using the Nested_Set gem in my code to sort Categories, Subcategories, and

  • 0

I am using the Nested_Set gem in my code to sort Categories, Subcategories, and Products. I am trying to limit the depth/level of my nested set to no deeper then 2. Currently I am getting the error

Nested Set Error undefined method `self_and_descendants' for #<ActiveRecord::Relation:0x52c4a30> 

I am trying to create a restraunt menu type style, and am going to attempt to make it drag and drop sortable.

Here is my code:
Can someone browse it and help me understand this error? Thanks
Category.rb

class Category < ActiveRecord::Base
  acts_as_nested_set
  acts_as_list :scope => :parent_id
  has_many :products
  scope :category, where("parent_id IS NULL")
  scope :subcategories, where("parent_id IS NOT NULL")
  scope :with_depth_below, lambda { |level| where(self.arel_table[:depth].lt(level)) }
end

categories_controller

class CategoriesController < ApplicationController
  def new
    @category = params[:id] ? Category.find(params[:id]).children.new : Category.new
    @count = Category.count
  end

  def new_subcategory
    @category = params[:id] ? Category.find(params[:id]).children.new : Category.new
    @category_2_deep = Category.with_depth_below(2)
  end

  def create
    @category = params[:id] ? Category.find(params[:id]).children.new(params[:category]) : Category.new(params[:category])
    if @category.save
      redirect_to products_path, :notice => "Category created! Woo Hoo!"
    else
      render "new"
    end
  end

  def edit
    @category = Category.find(params[:id]) 
  end

  def edit_subcategory
    @category = Category.find(params[:id]) 
    @category_2deep = Category.with_depth_below(2).arrange
  end

  def destroy
    @category = Category.find(params[:id])
    @category.destroy
    flash[:notice] = "Category has been obliterated!"
    redirect_to products_path
  end

  def update
    @category = Category.find(params[:id])
    if @category.update_attributes(params[:category])
      flash[:notice] = "Changed it for ya!"
      redirect_to products_path
    else 
      flash[:alert] = "Category has not been updated."
      render :action => "edit"
    end
  end

  def show
    @category = Category.find(params[:id])
  end

  def index
  end

  def sort
    params[:category].each_with_index do |id, index|
      Category.update_all({position: index+1}, {id: id})
    end
  end
end

Routes.rb

Jensenlocksmithing::Application.routes.draw do
  get "log_out" => "sessions#destroy", as: "log_out"
  get "log_in" => "sessions#new", as: "log_in"
  get "site/home"
  get "site/about_us"
  get "site/faq"
  get "site/discounts"
  get "site/services"
  get "site/contact_us"
  get "site/admin"
  get "site/posts"
  get "categories/new_subcategory"
  get "categories/edit_subcategory"

  resources :users
  resources :sessions
  resources :coupons
  resources :monthly_posts
  resources :categories do
    collection { post :sort }
    resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory]
  end
  resources :subcategories
  resources :products
  resources :reviews
  resources :faqs do
    collection { post :sort }
  end 

  root to: 'site#home'
end

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_2_deep, @category) {|i, level| "# {'-' * level if level < 1 } #{i.name if level < 1 }" } %>

</p>
<p>
<%= f.label(:position) %>
<%= f.select :position, 1..category_count %>
</p>
<p>
  <%= f.submit("Submit") %>
</p>
<% 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-30T23:41:47+00:00Added an answer on May 30, 2026 at 11:41 pm

    It looks like nested_set is looking for an Array not just an array-like collection – see line 32 of the source: https://github.com/skyeagle/nested_set/blob/21a009aec86911f5581147dd22de3c5d086355bb/lib/nested_set/helper.rb#L32

    …hence it’s getting that ActiveRecord::Relation, wrapping it in an [array] (line 35) and then trying to iterate and blowing up.

    Easy fix: call to_a on the collection first – in your controller:

    @category_2_deep = Category.with_depth_below(2).to_a
    

    Better fix: submit a patch to the maintainer that’s a bit more Ruby-like and looks for it to behave like an Array, but not necessarily be one.

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

Sidebar

Related Questions

I am trying to create my app using the Nested Set Gem here I
I'm using nested set model for my menu tree, and I'm trying to get
I'm using a nested set in a MySQL table to describe a hierarchy of
I'm using the nested set model that'll later be used to build a sitemap
I am using a nested set model and want to be able to select
I've got a Nested Set Model working for my site with items in subcategories
I am using the nested set model to store a large hierarchy of data
I am using a nested set to represent a hierarchy in my application, and
I've been using the crap out of the Nested Set Model lately. I have
I am using Ruby (Ruby on Rails) and have a nested set of about

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.