Subscribe
def search
@title = "Search"
if params[:q]
query = params[:q]
# First find the user hits...
@clients = Client.find_by_contents(query, :limit => :all)
# ...then the subhits.
tradenames = Tradename.find_by_contents(query, :limit => :all)
words = Word.find_by_contents(query, :limit => :all)
# Now combine into one list of distinct users sorted by last name.
hits = tradenames + words
@clients.concat(hits.collect { |hit| hit.clients }).uniq!
@clients = @clients.sort { |client| client.business_name }
end
end
<% form_tag({ :action => "search", :controller => "community" }, :method => "get") do %>
<fieldset>
<legend>Search</legend>
<dl>
<dt><label for="q">Search for:</label></dt>
<dd><%= text_field_tag "q", params[:q] %></dd>
</dl>
</fieldset>
<input id="submit" type="submit" value="Search" />
<% end %>
def search
@title = "Search"
if params[:q]
query = params[:q]
# First find the user hits...
@results1 = Tradename.find_by_contents(query, :models => [Tradename, Client], :limit => :all)
if @results1.empty?
@results2 = Word.find_by_contents(query, :models => [Word, Tradename, Client], :limit => :all)
if @results2.empty?
@results = Client.find_by_contents(query, :models => [Client], :limit => :all)
else
@results = @results2
end
else
@results = @results1
end
end
end
<% if @results and not @results.empty? %>
<table class="database_dump">
<tr class="header">
<th class="name">Business Name</th>
</tr>
<% @results.each do |result| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="name"><%= @results.id %></td>
</tr>
<% end %>
</table>
<% end %>
undefined method `business_name' for #<Tradename id: 1, title: "Plumber">or
undefined method `business_name' for #<Word id: 3, tradename_id: 1, term: "pipe">
case result
when Client
# client code here
when Tradename
# tradename code here
when Word
# word code here
end
<% if @results3 and not @results3.empty? %>
<% @results.each do |result| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="name"><%= result.business_name %></td>
<td class="profile"><%= link_to result.screen_name, profile_url(:screen_name => result.screen_name) %></td>
<td class="phone"><%= result.contact_phone %></td>
<td class="zip"><%= result.address_zip %></td>
</tr>
<% end %>
<% end %>
<% if @results1 and not @results1.empty? %>
<% @results.each do |result| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="name"><%= result.title %></td>
<td class="profile"><%= result.id %></td>
<td class="phone"> </td>
<td class="zip"> </td>
</tr>
<% end %>
<% end %>
You are not logged in, either login or create an account to post comments
Since you're using ferret and that's what it's for, make sure you've indexed all the fields you want to search in each model, then use this line to search:
@results = Client.find_by_contents("jemen", :models => :all)
It should search across all the models you've added indexes on. Good luck!
posted by cdmwebs at 9:03 AM on May 26