Nested Set Gem 是如何工作的,以及如何将其合并到我的项目中?

问题描述 投票:0回答:2

最近有人建议我,对于我当前的 Rails 应用程序关系,我应该使用 gem nested set。 (我之前的帖子/问题这里)我目前有 3 个模型,

类别有_许多子类别
子类别属于类别,并且有很多产品。
产品属于_子类别。我想像这样显示它

+类别
----子类别
--------产品
--------产品
----子类别
--------产品
--------产品

+类别
----子类别
--------产品
--------产品

那么如果我要在nested_set中执行此操作,我将如何在我的模型中进行设置?我是否可以删除子类别和产品模型,然后在类别模型中添加 actions_as_nested_set ?一旦我处理好模型,我将用什么来更新我的控制器操作,以便能够在我创建的嵌套集中创建节点?

我想只是帮助我理解如何执行 CRUD、创建、读取、更新和销毁这个nested_set 列表。

这是我已经有的一些代码

类别控制器:

class CategoriesController < ApplicationController
def new
  @category = Category.new
  @count = Category.count
end

def create
@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 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
  @categories = Category.all
end 
end

类别型号:

class Category < ActiveRecord::Base
  acts_as_nested_set
  has_many :subcategories
  validates_uniqueness_of :position
  scope :position, order("position asc")

end

子类型号:

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
  scope :position, order("position asc")
end

最后是产品型号:

class Product < ActiveRecord::Base
  belongs_to :subcategory
  has_many :products
  scope :position, order("position asc")
end

任何帮助将不胜感激。

ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 nested-sets
2个回答
2
投票

我会选择一个类别和一个产品,如下所示:

class Product > ActiveRecord::Base
  belongs_to :category
end

class Category > ActiveRecord::Base
  has_many :products
  acts_as_nested_set
end

class CategoryController < ApplicationController
   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 new
      @category = params[:id] ? Category.find(params[:id]).children.new : Category.new
   end

   def index
      @categories = params[:id] ? Category.find(params[:id]).children : Category.all
   end
end

#config/routes.rb your categories resource could be something like..
resources :categories do
   resources :children, :controller => :categories, 
                              :only => [:index, :new, :create]
end

这种方式是最灵活的,因为你可以将你的产品放在任何级别的任何类别中。


0
投票

2024 年及以后的任何人都可以使用 https://github.com/collectiveidea/awesome_nested_set 因为它得到积极维护

© www.soinside.com 2019 - 2024. All rights reserved.