我有两个期刊模型,ClientJournal
和 InstitutionJournal
,我正在使用 Sunspot 运行一个查询,该查询获取记录以将它们一起显示在期刊索引页上:
search = Sunspot.search ClientJournal, InstitutionJournal do
paginate page: params[:page], per_page: 30
order_by :created_at, :desc
end
这几乎可以工作,但它在排序之前将结果分成两个类,因此 search.results
返回如下内容:
created_at class
09:30 ClientJournal
09:12 ClientJournal
08:57 ClientJournal
07:32 ClientJournal
09:45 InstitutionJournal
09:22 InstitutionJournal
09:07 InstitutionJournal
当我真正想要的是:
created_at class
09:45 InstitutionJournal
09:30 ClientJournal
09:22 InstitutionJournal
09:12 ClientJournal
09:07 InstitutionJournal
08:57 ClientJournal
07:32 ClientJournal
有什么方法可以告诉 Sunspot 将结果排序在一起,就好像它们是同一模型一样?
最佳答案
你可以这样做来搜索多个模型并首先获取单个模型数组,然后像我一样对它们进行排序:-
search = Sunspot.search ClientJournal,InstitutionJournal do
keywords(params[:search])
#fulltext params[:search]
order_by(:created_at, :desc)
paginate :page => params[:page], :per_page => 10
end
##group them by class
@results = search.results.group_by(&:class)
##get all clientjournals in created_at order
@clientjournals= @results[ClientJournal]
##OR do anything that you want with the array of cilentjournals
@clientjournals= @clientjournals.sort_by { |a| a.created_at }
关于ruby-on-rails - 如何为多个模型的查询排序 Sunspot 搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074985/