【发布时间】:2012-08-08 03:02:34
【问题描述】:
我有属于某个类别的产品。类别构成一棵树 通过让父母和孩子使用自联接来构建结构:
协会:
class Category < ActiveRecord::Base
has_many :children, class_name: "Category", foreign_key: "parent_id"
belongs_to :parent, class_name: "Category"
end
class Product < ActiveRecord::Base
belongs_to :category
end
例如,
Fruits & Vegetables => "High" Category
Fresh Fruits => "Intermediate" Category
Citrus => "Low" Category
Limes Large => Product
我想使用 Thinking Sphinx 来索引“低”类别名称和 产品的“高”类别名称,甚至可能是树层次结构中介于两者之间的所有类别名称。
索引低类别父名称没有问题,如下所示:
class Product < ActiveRecord::Base
indexes :name
indexes category.parent.name, as: :low_category
end
注意:“高”和“低”类别之间的节点数是可变的。我需要一种动态添加分层名称的方法。
但是如何在树中进一步索引类别名称?我知道我不能使用方法 在 TS 索引中,我该如何设置数据库?
最重要的是,如何索引“高”类别名称?
【问题讨论】:
标签: ruby-on-rails tree indexing associations thinking-sphinx