I have seen a few posts here about searching across multiple models (here and here, in particular). However, I'm wondering if I can adapt Ryan's Railscast #37 to do this for three or four models without having to figure out Thinking Sphinx as I'm a beginner to all this.
As I said I have a few models, although I only reference two for brevity below, and I made a searches_controller
with index
and show
actions.
On the models I included Ryan's model code:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
And in the searches_controller
I have the following:
def index
@profiles = Profile.search(params[:search])
@employers = Employer.search(params[:search])
end
def show
@profiles = Profile.search(params[:search])
@employers = Employer.search(params[:search])
end
In my searches/show.html.erb
I have the following:
<%= @profiles.each do |profile| %>
<div class="question">
<div class="questionHeader">
<h5 class="questionTitle"><%= link_to profile.first_name, profile %></h5>
</div>
</div><!-- end question -->
<% end %>
<%= @employers.each do |employer| %>
<div class="question">
<div class="questionHeader">
<h5 class="questionTitle"><%= link_to employer.name, employer %></h5>
</div>
</div><!-- end question -->
<% end %>
When I perform the search, my show renders what looks like three empty arrays [][][]
.
Without ditching this and moving to Thinking Sphinx is there anything I can do to get this working using the simple form below?
<% form_tag searches_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "#", :name => nil %>
</p>
<% end %>