我有一些问题,我会试着解释一下。
我想要建立分类,提供这样的功能。我有一些大的分类,比如垃圾回收设备,下面是子分类,比如直升机,然后我又有了子分类。
我想建立分类系统,当我创建新产品时,可以选择这样的分类路线。
我想知道为我的应用程序提供此类功能的最佳方式是什么?
有什么建议吗?
acts_as_taggable提供这样的功能吗?
谢谢!
发布于 2013-02-20 20:48:35
这至少会在模型层中为您所说的树结构提供支持。
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 sub1https://stackoverflow.com/questions/14980110
复制相似问题