2012-02-27 47 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

相關問題