I’m implementing search based on the recent Railscast on Thinking
Sphinx. I have a reasonably complex tabbed interface for the results.
The searches controller handles the searches, and :collections, like
:comments => :get, handle when the user wants to break down the results
in to a category (the interface has a tab for each model), giving urls
like /searches/comments/:search. My searches controller only performs
one find in a before_filter called load_results, so when a user clicks
on a category tab, the params[:search] is passed over in the link_to for
the tab and the before_filter loads the same results (am I right in
thinking that the controller is only performing the search once, or do I
need to do more to ‘cache’ the load_results in the before filter? I’m
aiming to only do one search the entire time the user in in the searches
controller, and to rip out only the models we need for each :collection.
So far, I haven’t worked out how to, a) combine the model searches in to
one @results variable in the before filter, and, b) how to only take out
‘result in @results’ for the :collections for each model;
a) something like this?
private
def load_results
@results = Comment.search params[:search] + User.search
params[:search]
end
b) my views are doing logic which probably belongs in the controller
actions,
I don’t know how to do an equivalent conditional in the controller
action…
<% for result in @results %>
<% if result.class.name == “Comment” %>
<%= render :partial => “comments/comment”, :locals => { :comment =>
result } %>
<% end %>
<% end %>
Any ideas on either of these?