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

发布于 2025-01-06 06:24:11 字数 2262 浏览 3 评论 0原文

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

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

+类别
----子类别
--------产品展示
--------产品展示
----子类别
--------产品展示
--------产品展示

+类别
----子类别
--------产品展示
--------产品

那么,如果我要在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

任何帮助将不胜感激。

I have recently been advised that for my current rails app relationships I should use the gem nested set. ( My previous thread / question here) I currently have 3 models,

Categories has_many Subcategories
Subcategories belongs_to Categories, and has_many products.
Product belongs_to Subcategory. I wanted to display it something like this

+Category

----Subcategory

--------Product

--------Product

----Subcategory

--------Product

--------Product

+Category

----Subcategory

--------Product

--------Product

So if I were to do this in nested_set, how would I set this up in my Models? Would I remove my subcategory and product models, and just add acts_as_nested_set in the Category model? and once I have the model taken care of, what will I update my controllers actions with, to be able to create nodes in the nested set I create?

I guess just help me understand how I can do the CRUD, create, read, update, and destroying of this nested_set list.

Here is some code I have already

Categories Controller:

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

Category Model:

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

end

Subcategory Model:

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

And finally, Product Model:

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

Any help would be very appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2025-01-13 06:24:11

我会像这样使用一个类别和一个产品:

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

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

I would go with a Category and a Product like so:

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

this way is the most flexible, as you can put your products in any a category at any level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文