问题
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:
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.
来源:https://stackoverflow.com/questions/9459662/nested-set-error-undefined-method-self-and-descendants-for-activerecordrel