0

我有一些问题,我会尽力解释。

我想建立分类,提供这样的功能。我有一些大类别,例如废物回收设备,下面是子类别,例如切碎机,然后我又有子类别。

我想建立分类系统,允许当我创建新产品时选择这样的分类路径。

我想知道为我的应用程序提供此类功能的最佳方式是什么?

有什么建议么?

act_as_taggable 提供这样的功能吗?

谢谢 !

4

1 回答 1

2

我已经将宝石血统用于类似目的。这至少会在模型层中为您正在谈论的树结构提供支持。

RailsCasts已经完成了一个关于它的教程。

要与祖先一起玩,我建议使用rails console,例如:

# category.rb

class Category < ActiveRecord::Base
  attr_accessor :name, :parent
  has_ancestry
end

# rails console

~/Rails/CTK/jwbc[master] $ rails console
Loading development environment (Rails 3.2.11)
1.9.3p286 > main = Category.create(name: "Main category")
# => created
1.9.3p286 > sub1 = Category.create(name: "First subcategory", parent: main)
# => created
1.9.3p286 > sub2 = Category.create(name: "Sub-subcategory"), parent: sub1)
# => created
1.9.3p286 > main.children
# => Would return sub1
1.9.3p286 > main.descendants
# => Returns sub1 and sub2
1.9.3p286 > Category.at_depth(1)
# => Returns all subcategories, in this case sub1
于 2013-02-20T12:48:34.503 回答