Reputation: 1736
I have two journal models, ClientJournal
and InstitutionJournal
and I'm using Sunspot to run a query which grabs records to display them together on the journals index page:
search = Sunspot.search ClientJournal, InstitutionJournal do
paginate page: params[:page], per_page: 30
order_by :created_at, :desc
end
This almost works but it splits the results into the two classes before ordering them, so search.results
returns something like this:
created_at class
09:30 ClientJournal
09:12 ClientJournal
08:57 ClientJournal
07:32 ClientJournal
09:45 InstitutionJournal
09:22 InstitutionJournal
09:07 InstitutionJournal
When what I actually want is this:
created_at class
09:45 InstitutionJournal
09:30 ClientJournal
09:22 InstitutionJournal
09:12 ClientJournal
09:07 InstitutionJournal
08:57 ClientJournal
07:32 ClientJournal
Is there any way of telling Sunspot to order the results together as if they were the same model?
Upvotes: 3
Views: 351
Reputation: 5112
you can do something like this to search multiple models and first get individual model array and then sort them as i do:-
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 }
Upvotes: 0