Hi, Pat! Thanks, that did the trick! It might get a bit unwieldy though - I was just starting out with three fields to get things working, but I'm going to end up searching on lots of fields, many of which are in related tables... I will no doubt have more questions as I go. Thanks for your help! Morgan.
On Oct 28, 10:20 pm, Pat Allan <[email protected]> wrote: > Hi Morgan > > With three fields, you're going to want to link each param to the appropriate > field in your index. So, with that in mind, the code might look a little like > the following: > > def index > Source.search :conditions => field_conditions, :page => params[:page] > end > > private > > def field_conditions > %w( text_name editor region ).inject({}) do |hash, field| > hash[field.to_s] = params[field] if params[field].present? > hash > end > end > > This will build up the conditions hash only when there's actually text to > search for in that field. > > Let me know if this isn't clear, or if you have more questions :) > > -- > Pat > > On 29/10/2010, at 10:10 AM, Morgan Kay wrote: > > > I'm new to Thinking Sphinx, and have been a little stymied because > > there aren't many tutorials out there. I have two search forms on my > > site. One just searches one field, so that's super easy, and I have > > that up and working just fine. The other is more difficult, because I > > need to search several fields. I'm starting with just three for now. > > > Here's my model: > > --------------------------------Source.rb---------------------------- > > class Source < ActiveRecord::Base > > > define_index do > > indexes :text_name > > indexes :editor > > indexes :region > > > has created_at, updated_at > > end > > > end > > -------------------------------------------------------------------------- > > > Here's the controller: > > ------------------------Sources_controller.rb-------------------- > > class SourcesController < ApplicationController > > > def index > > �...@sources = Source.search( (params[:search] || ""), :page=> > > (params[:page] || 1)) > > end > > > ... > > --------------------------------------------------------------------------- > > > And the view: > > ------------------------index.html.erb------------------------------ > > <% unless @sources.empty? %> > > <div id="results"> > > <%= render :partial => "results" %> > > </div> > > <% end %> > > > <% form_tag "", :method => get do -%> > > <fieldset> > > Text Name: <%= text_field_tag :text_name %><br /> > > Editor: <%= text_field_tag :editor %><br /> > > County/Region: <%= text_field_tag :region %> > > </fieldset> > > <%= submit_tag "Search" %> > > <% end -%> > > -------------------------------------------------------------------------- > > > I know that the problem is in my text_field_tags. On the form where I > > am just searching one field, I have "text_field_tag :search", and that > > works just fine, but when I am searching multiple fields, what > > parameters should I put in the text_field_tag? > > > For what it's worth, when I do a search using this form, the URL > > contains the right parameters (http://localhost:3000/sources? > > text_name=canterbury&editor=®ion=&commit=Search), but it shows all > > of the records, not just the ones that match the search criteria. > > > What do I need to be doing differently? > > > Thank you! > > Morgan. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Thinking Sphinx" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group > > athttp://groups.google.com/group/thinking-sphinx?hl=en. -- You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
