Tag: 嵌套集

嵌套设置错误未定义方法`self_and_descendants’用于#

我在我的代码中使用Nested_Set gem来对Categories,Subcategories和Products进行排序。 我试图将我的嵌套集的深度/级别限制为不再深入2.目前我收到错误 Nested Set Error undefined method `self_and_descendants’ for # 我正在尝试创建一个限制菜单类型样式,我将尝试使其拖放可排序。 这是我的代码:有人可以浏览它并帮助我理解这个错误吗? 谢谢Category.rb class Category :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 “Category created! Woo Hoo!” else render “new” end end def edit @category = Category.find(params[:id]) […]

Ruby on Rails – 令人敬畏的嵌套集插件

使用此插件时是否有一种简单的方法来显示整个嵌套集? 我想要做的是显示一个无序的根节点列表,其中每个子节点中的另一个无序列表也包含子节点等等? 任何建议表示赞赏 谢谢。

获取所有类别和子类别的产品(rails,awesome_nested_set)

正在开发一个正在开发的电子商务应用程序我试图解决以下问题:我通过awesome_nested_set插件实现了我的类别。 如果我通过选择一个类别列出我的文章一切正常,但对于某些链接,我想显示一个类别的所有产品和其子类别的产品。 这里只有一个类别的控制器代码可以正常工作: # products_controller.rb def index if params[:category] @category = Category.find(params[:category]) #@products = @category.product_list @products = @category.products else @category = false @products = Product.scoped end @products = @products.where(“title like ?”, “%” + params[:title] + “%”) if params[:title] @products = @products.order(“created_at).page(params[:page]).per( params[:per_page] ? params[:per_page] : 25) @categories = Category.all end 我注释掉的一行是我在类别模型中自己编写的辅助方法,它返回类别中所有产品及其子类别的数组。 它的定义如下: # app/models/category.rb def […]

如何将嵌套集中的所有记录呈现为真正的html树

我在我的Rails项目中使用了awesome_nested_set插件。 我有两个看起来像这样的模型(简化): class Customer < ActiveRecord::Base has_many :categories end class Category :customer_id validates_presence_of :name # Further validations… end 数据库中的树按预期构造。 parent_id , lft和rgt所有值都是正确的。 树有多个根节点(当然在awesome_nested_set允许)。 现在,我想在一个正确排序的树中呈现给定客户的所有类别,例如结构:例如嵌套的 标签。 这不会太难,但我需要它才能有效(sql查询越少越好)。 更新:计算出可以在没有进一步SQL查询的情况下计算树中任何给定节点的子节点数: number_of_children = (node.rgt – node.lft – 1)/2 。 这并不能解决问题,但可能会有所帮助。