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

JZ.*_*JZ. 5 sorting sphinx ruby-on-rails

我正在尝试使用传递给我的控制器的参数来完成一个简单的排序.我正在关注Searching&Thinking Sphinx网站上的文档,我遇到了以下错误.我究竟做错了什么?

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

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

ThinkingSphinx::SphinxError (index place_core: sort-by attribute 'created_at' not found):
Run Code Online (Sandbox Code Playgroud)

Mat*_*ani 7

您需要添加要搜索的字段.然后,通过一个字段进行排序,你需要将其标记为可排序的模型,或者您需要在添加属性define_index方法,这里解释.

对于您的模型,这样的事情:

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
Run Code Online (Sandbox Code Playgroud)

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