0

I have two models, an School model and Price model. Every school has a price. I would like to return in the search result school with its prices. I am using rails and sunspot.

School-controller:

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

School-model:

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

Index - view

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

How do I return for every school its prices, with sunspot?

1 Answers1

0

Maybe you can do eager loading with :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

Additionnal:

If a School can only have one Price, you should replace has_many :prices with has_one :price in your School model. After doing this, you could access to the price you want by doing this: result.price.min (take a look at number_to_currency, you could be interested in this Helper)

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • It it workt with this ":include => :prices" but it returns price results like this: # –  Jan 25 '13 at 16:20
  • Well yea it eager loads the dependent prices of the particular School. `result.prices` will list all the prices of this school. Then you need to 'select' the one you are interesting in (as I can see, the min) and then display the appropriate attribute. If you don't get it, just post the structure of your Price model and which price of a School you want to show. – MrYoshiji Jan 25 '13 at 16:24
  • Ok, how do I 'select' specifik column. How do I do that? Here is a gist of price table: https://gist.github.com/4635776 and the model: https://gist.github.com/4635785 –  Jan 25 '13 at 16:29
  • What is your strategy about selecting the school's price you want to show? Do you want the lowest price? The Highest? Which column of the Price table contains the actual attribute you want to show? I'm saying that because a School has multiple Prices, and I guess you want to show just one of them – MrYoshiji Jan 25 '13 at 16:32
  • In my case every schools have only one specfik price. Its schools for drivning. So it dosent have diffrent prices, it have specifik prices for course books, driving lincense fee and more. check this gist with comments: https://gist.github.com/4635776 –  Jan 25 '13 at 16:40
  • If a School can only have one Price, you should replace `has_many :prices` with `has_one :price` in your School model. After doing this, you could access to the price you want by doing this: `result.price.min` (take a look at http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency, you could be interested in this Helper ) – MrYoshiji Jan 25 '13 at 16:42
  • Can you help my with this question: http://stackoverflow.com/questions/14541528/sorting-the-result-sunspot-rails –  Jan 27 '13 at 15:45