我有两个模型,School
模型和Price
模型。每个学校都有价格。现在我得到了我想要的结果,但我喜欢实现一个排序功能,对学校的最高价格和最低价格进行排序。
学校控制器:
class SchoolsController < ApplicationController
def index
@query = params[:search]
@search = School.search(:include => [:price] do
fulltext params[:search]
paginate :page => params[:page], :per_page => 7
end
@results = @search.results
end
end
学校模式:
class School < ActiveRecord::Base
has_one :price
# sunspot search
searchable do
text :name, :locality
end
end
索引 - 视图
<ul>
# Cheapest price
<li><span class="label label-ord">Show cheapest price <%= @query %></span></li>
# Highest price
<li><span class="label label-ord">Show highest price <%= @query %></span></li>
</ul>
<% for result in @results %>
<tr>
# School name, from the school-model
<td><h3><%= link_to result.name, result %></h3></td>
# School price, from the price-model
<td><h3><%= result.prices.min %> kr</h3></td>
</tr>
<% end %>
如何排序最高价和最低价;我应该使用方面还是方法?我怎么做?