Ok so i have a graphic model and I am using thinking sphinx as the search tool. It works well but i want to display different models on the search results page.. for example
i have this in my Graphic model
define_index do
indexes :name, :description, :scale,
indexes sub_category.name, :as => :subcategory_name
indexes sub_category.category.name, :as => :category_name
indexes colors.name, :as => :color_name
end
This is fine and good but the problem is i want to display all the categories and subcategories for a found search and not just the graphics that are related. In my controller should i have three find like
@graphics = Graphic.search params[:search]
@categories = Categories.search params[:search]
@sub_categories = SubCategories.search params[:search]
this seems like overkill...is there a better way so in the view i can show each of them seperately
You'll need to have indexes defined in your Category and SubCategory models as well, and then you can search across all three at once:
@results = ThinkingSphinx.search params[:search], :page => params[:page]
In your view, you'll want some logic around each search result to render the correct HTML - perhaps you can have different partials for each class? I'd also recommend wrapping it into a helper. Here's a start:
<ul>
<% @results.each do |result| %>
<li><%= render :partial => partial_for_search_result(result),
:locals => {:result => result} %></li>
<% end %>
</ul>
And the helper:
def partial_for_search_result(result)
case result
when Graphic
'graphics/search_result'
when Category
'categories/search_result'
when SubCategory
'sub_categories/search_result'
else
raise "Unknown search result/partial mapping for #{result.class}"
end
end
Hopefully this gives you some ideas on how to approach the problem.