2014-09-27 58 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