2014-09-27 86 views
3

我有两个日记模型,ClientJournalInstitutionJournal和我使用的太阳黑子运行哪个抓住记录的查询的期刊索引页上同时显示它们:如何订购太阳黑子多种型号查询的搜索结果?

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 

是否有任何方法告诉太阳黑子将结果排列在一起,就好像它们是同一个模型一样?

+0

你有没有想出解决办法? – jmgem 2015-08-27 05:34:41

回答

0

你可以做这样的事情来搜索多种型号的首先获得个人模式阵列,然后对其进行排序,因为我做的: -

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 } 
+0

与此相关的问题是,您只对一个页面的10个结果进行排序。如果您从搜索块中删除'paginate'以避免出现这种情况,那么您最终可能会得到数千个结果,而这些结果的排序真的很慢。 – Simon 2014-09-27 14:41:31