Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails思考Sphinx索引自连接关联树结构_Ruby On Rails_Tree_Indexing_Associations_Thinking Sphinx - Fatal编程技术网

Ruby on rails Rails思考Sphinx索引自连接关联树结构

Ruby on rails Rails思考Sphinx索引自连接关联树结构,ruby-on-rails,tree,indexing,associations,thinking-sphinx,Ruby On Rails,Tree,Indexing,Associations,Thinking Sphinx,我有属于某一类别的产品。类别组成一棵树 通过让父级和子级使用自联接来构造: 协会: 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_

我有属于某一类别的产品。类别组成一棵树 通过让父级和子级使用自联接来构造:

协会:

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
我想使用Thinking Sphinx来索引“low”类别名称和 产品的“高”类别名称,甚至可能是树层次结构中介于两者之间的所有类别名称

我对低类别父名称的索引没有问题,如下所示:

class Product < ActiveRecord::Base
  indexes :name
  indexes category.parent.name, as: :low_category
end
类产品
注意:“高”和“低”类别之间的节点数是可变的。我需要一种动态添加分层名称的方法

但是,我该如何在树的更高层为类别名称编制索引呢?我知道我不能使用方法 在TS索引中,如何设置数据库

最重要的是,如何为“高”类别名称编制索引?

您可以这样做吗

class Product < ActiveRecord::Base
  indexes :name

  category = category.parent
  indexes category.name, as: :low_category

  while category.parent do
    if category.parent
      indexes category.name, as: :root_category
    elsif category.parent
      indexes category.name, as: :high_category
    else
      indexes category.name
    end

    category = category.parent
  end
end
类产品
您能这样做吗

class Product < ActiveRecord::Base
  indexes :name

  category = category.parent
  indexes category.name, as: :low_category

  while category.parent do
    if category.parent
      indexes category.name, as: :root_category
    elsif category.parent
      indexes category.name, as: :high_category
    else
      indexes category.name
    end

    category = category.parent
  end
end
类产品
是,这适用于该特定情况,但高类别和低类别之间的节点数是可变的。我需要动态生成这些名称,并将高级类别名称与所有其他名称分开。有没有办法在TS中这样做?我改变了答案。这可能不是更好的解决方案,但它应该可以工作。是的,这在特定情况下可以工作,但是高类别和低类别之间的节点数量是可变的。我需要动态生成这些名称,并将高级类别名称与所有其他名称分开。有没有办法在TS中这样做?我改变了答案。这可能不是更好的解决方案,但它应该有效。