I'm so close to finishing my new database project, but I'm bumping up against my personal ceiling. Can anyone help me determine how to search my Rails project?
Background Server Information:
- Instant Rails 2.0
- Ruby 1.8.6 Patch Level 111
- Upgrades Rails to 2.0.2
- MySQL 5.0
- Ferret 0.11.6
- Acts As Ferret 0.4.3
I've got what I think is a simple database setup, but cannot figure out how to search the project. I've got my tables created, forms are accepting data, but I can't seem to search.
This is the last piece of the project, so I'm itching for an answer. Any help would be greatly appreciated! I'll try to err on the side of 'too much' information.
Tables:
- Clients (ID, username, business_name, ... , Tradename_ID)
- Tradenames (ID and title)
- Words (ID, term, and Tradename_ID)
Links:
- Clients have a Tradename_ID
- Words have a Tradename_ID
Relations:
- Each Client has a Tradename (has_one :tradenames)
- Each Word has a Tradename (has_one :tradenames)
- Each Tradename has many Clients (has_many :clients)
- Each Tradename has many Words (has_many :clients)
I would like to have a search box that searches the entire project. It should return results that meet any of the following criteria:
- If the search term returns a Tradename, return all Clients with that Tradename ID
- If the search term returns a Word, return all Clients with that linked Tradename ID
- If the search term doesn't match a Tradename or Word, then search the Clients business_name for a match. If found, return any Clients with a matching business_name
Currently, I've got another model called 'Community' that is acting as a hub for all the information. The current search model (from app\models\community.rb) looks like this:
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
This is the code that is doing the searching (in app\views\community\_search_form.rhtml):
<% 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 %>
Any ideas? Should I be joining all of these into a single table? Some sort of recursive search? If so, how?
(I'm Leia in the 'Help me Obi-Wan Kenobi, you're my only hope' analogy.)
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, 2008