0

Schoolモデルとモデルの2つのモデルがありPriceます。すべての学校には価格があります。その価格で検索結果学校に戻りたいと思います。レールとサンスポットを使用しています。

学校の管理者:

class SchoolsController < ApplicationController
def index
 @query = params[:search]
 @search = School.search do 
   fulltext params[:search]
     paginate :page => params[:page], :per_page => 7
   end
 @results = @search.results
end
end

学校モデル:

class School < ActiveRecord::Base
 has_many :prices
 # sunspot search
  searchable do
   text :name, :locality
  end
end

インデックス-表示

<% 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 %>

黒点を使用して、すべての学校の価格を返すにはどうすればよいですか?

4

1 に答える 1

0

たぶんあなたは熱心な読み込みを行うことができます:include

@search = School.search(:include => :prices) do # or :include => :price, depends on the relation
 fulltext params[:search]
 paginate :page => params[:page], :per_page => 7
end

追加:

学校が1つの価格しか持てない場合は、学校モデルでにhas_many :prices置き換える必要があります。has_one :priceこれを行った後、これを行うことで希望の価格にアクセスできます:( number_to_currencyresult.price.minを見てください、このヘルパーに興味があるかもしれません)

于 2013-01-25T16:08:51.353 に答える