2012-02-27 84 views
0

我在我的代码中使用了Nested_Set gem来对类别,子类别和产品进行排序。我想我的嵌套组的深度/水平限制,不得更深然后2.目前我正在嵌套集错误未定义的方法`self_and_descendants'为#<ActiveRecord :: Relation:0x52c4a30>

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

我想创建一个餐馆名称菜单式风格,我要尝试做出错误它拖放可排序。

这是我的代码: 有人可以浏览它并帮我理解这个错误吗?由于 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 

类别/ 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 %> 

回答

2

看起来nested_set期待为 数组不仅仅是一个类似数组的集合 - 请参阅源代码的第32行:https://github.com/skyeagle/nested_set/blob/21a009aec86911f5581147dd22de3c5d086355bb/lib/nested_set/helper.rb#L32

...因此,它获得了ActiveRecord :: Relation,将其封装在[数组]中(第35行),然后尝试迭代吹起来。

容易修复:呼吁收集to_a第一 - 在您的控制器:

@category_2_deep = Category.with_depth_below(2).to_a 

更好的修复:提交补丁到这是一个有点更红宝石般,看起来它的维护者的行为类似一个数组,但不一定是一个。

+0

工作。再次感谢! – ruevaughn 2012-03-22 04:59:16

+0

我还有一个问题,文档说'没有理由使用深度列。这只是增加额外的查询数据库没有好处。如果你需要关卡,你应该使用each_with_level。'所以如何在没有深度栏的情况下做到这一点? – ruevaughn 2012-04-11 22:46:10

+0

随时浏览我的新问题[这里](http://stackoverflow.com/questions/10116481/need-to-understand-code-that-finds-depth-of-nested-set-in-rails-3-应用程序)。我仍然有问题,因为我不能只调用我没有深度栏的范​​围。 – ruevaughn 2012-04-12 02:21:45

相关问题