获取thinking_sphinx定义的字段和属性

时间:2011-09-04 11:53:26

标签: ruby-on-rails ruby-on-rails-3 thinking-sphinx

如何在模型上获得已定义的thinking_sphinx属性和字段列表? 特别是我想检查是否为sphinx

定义了输入属性

示例定义:

has store.name, :as => :store_name

我想要Model.sphinx_attributes.include? :store_name

之类的东西

1 个答案:

答案 0 :(得分:2)

以下应该可以解决问题:

Model.sphinx_indexes.collect { |index|
  index.attributes.collect &:unique_name
}.flatten.include?(:store_name)

字段大致相同:

Model.sphinx_indexes.collect { |index|
  index.fields.collect &:unique_name
}

在运行之前,最好先运行它,以确保加载索引:

Model.define_indexes

如果它们已经加载,那么该方法将无效。