你如何在Rails 3上使用Thinking Sphinx排序?

时间:2011-05-14 00:15:08

标签: ruby-on-rails sorting sphinx

我正在尝试使用传递给我的控制器的参数来完成一个简单的排序。我正在关注搜索和放大器上的documentation思考Sphinx网站,我遇到了以下错误。我做错了什么?

以下@places对象是think Sphinx类的一个实例。

 @places = Place.search(params[:q], :order => :created_at)

ThinkingSphinx::SphinxError (index place_core: sort-by attribute 'created_at' not found):

1 个答案:

答案 0 :(得分:7)

您需要添加要搜索的字段。然后按字段排序,您需要在模型中将其标记为可排序,或者您需要在 define_index 方法中添加属性explained here

对于您的模型,如下所示:

class Place < ActiveRecord::Base
  # ...

  define_index do
    # fields
    indexes subject, :sortable => true
    indexes content
    indexes author.name, :as => :author, :sortable => true

    # attributes
    has created_at
  end

  # ...
end

在该示例中,subject,author和created_at是可排序的。