【发布时间】:2013-01-26 21:00:51
【问题描述】:
我有两个模型,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 %>
如何排序最高价和最低价;我应该使用方面还是方法?我该怎么做?
【问题讨论】:
-
@MrYoshiji - 有什么想法吗?或文档链接?
-
您的问题模棱两可。您想获得所有结果的最高和最低价格,还是要按价格升序/降序对结果进行排序?此外,您希望它们成为所有搜索结果中的最高/最低价格,还是仅在分页结果中?
-
@m_x 对所有结果进行升序和降序排序。
标签: ruby-on-rails ruby ruby-on-rails-3 sunspot sunspot-rails