0

I have two models, an School model, a Price model. A School has one price.

Here is my School-controller, I eager load the price model with school-model:

def index
@search = School.search(:include => [:price]) do  
   fulltext params[:search]
   paginate :page => params[:page], :per_page => 7
end
 @results = @search.results
end

In my School model I do something like this:

searchable do
  string :name 
  text :locality
end

In sunspot you can use a method for sorting and ordering the search result: https://github.com/sunspot/sunspot#ordering
Very easy if you want order the result by the name of a School, you do somehting like this:

@search = School.search(:include => [:price]) do  
  fulltext params[:search]
  order_by(:name, :desc)
  paginate :page => params[:page], :per_page => 7
end

But in my case I would like to order by a column in the price model. How do I do that in sunspot?

  • Possible duplicate of http://stackoverflow.com/questions/14204316/rails3-solr-sort-order-using-a-child-field – Noz Jan 29 '13 at 17:14
  • @Cyle Not a clear answer. Can you help, have any ideas? –  Jan 29 '13 at 19:25
  • I haven't done any ordering through Sunspot, but the `results` method returns an ActiveRecord array, correct? Can't you simply order the results? Not sure if that would make an extra DB call, might want to try looking at the logs for details and see if your database is getting additional hits. – Noz Jan 29 '13 at 20:33
  • @Cyle I have thought about that, but I get only the School data and not the Price data beacuse I eager load the price model. It just feels stupid that there is not clear documentation about such simple thing.. –  Jan 29 '13 at 22:58

0 Answers0