嵌套设置错误未定义方法`self_and_descendants’用于#

我在我的代码中使用Nested_Set gem来对Categories,Subcategories和Products进行排序。 我试图将我的嵌套集的深度/级别限制为不再深入2.目前我收到错误

Nested Set Error undefined method `self_and_descendants' for # 

我正在尝试创建一个限制菜单类型样式,我将尝试使其拖放可排序。

这是我的代码:有人可以浏览它并帮助我理解这个错误吗? 谢谢Category.rb

 class Category  :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  "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 

类别/ form.html.erb

  

<%= f.select :parent_id, nested_set_options(@category_2_deep, @category) {|i, level| "# {'-' * level if level < 1 } #{i.name if level

看起来nested_set正在寻找一个数组而不仅仅是一个类似于数组的集合 – 参见源代码的第32行: https : //github.com/skyeagle/nested_set/blob/21a009aec86911f5581147dd22de3c5d086355bb/lib/nested_set/helper.rb#L32

…因此它得到了ActiveRecord :: Relation,将它包装在[数组]中(第35行)然后尝试迭代并炸毁。

轻松修复to_a在集合中调用to_a – 在您的控制器中:

 @category_2_deep = Category.with_depth_below(2).to_a 

更好的解决方法 :向维护者提交一个补丁,它更像Ruby,并且看起来一个Array,但不一定是一个。