I have a page that when visted brings up the most recently active users. Above the users are some filtering options such as filtering by one or a combination of:
- location
- gender
- sexual preference
- age range
- country
I'm using a form_tag helper for my form. My issue is passing each of these parameters to my controller:
class BrowsersController < ApplicationController
def index
@default_image = "/assets/default_avatar.jpg"
@users = Profile.search params[:search], :page => params[:page], :per_page => 26
end
end
If I was searching with one field with the param "Search" I would be fine but I have multiple fields, select menu's on my form. How am I suppose to pass that info to my controller in order to filter the search results?
I'm sure I'm not the first to use search filtering in ruby on rails
<%= form_tag browsers_path, :method => 'get' do %>
<p>
Location: <%= text_field_tag :location %><br />
Gender: <%= select_tag :gender,
options_for_select([["Select", nil],
["Male", 1],
["Female", 2]]) %>
<br />
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<br />
Kind regards
update
@users = Profile.search 'HERE IS WHERE THE POWER LIES', :page => params[:page], :per_page => 20, :conditions_all => { :gender => params[:gender], :location => params[:location]}
I use :conditions_all to get my field params and in rails server logs I can see that they are being picked up.. now I just need to some how get them all seen by thinking sphinx
Update 2 i have has gender in the define_index block and indexes location because it seems i need at least 1 field.
this working to return genders:
@users = Profile.search params[:location], :page => params[:page], :per_page => 40, :with => { :gender => [params[:gender]]}
I've tried to check for both location and gender and it seems to work but I'll double check in console because it's returning 1 female in united kingdom out of 1000 and that could be wrong but I'll double check in console and edit this update appropriately.
I'm not quite sure where you got
:conditions_all
from - if you're dealing with fields, then:conditions
is what you're after:But, it sounds like you've got gender as an attribute instead of a field - and so, you want to filter on it instead:
As I said here, I will try to explain a bit of Thinking Sphinx and my suggested approach to solve your problem.
Let's say you have the following Profile model:
And you may have those models: class SexualPreference < ActiveRecord::Base has_and_belongs_to_many :profiles end
Then you may define your ThinkingSphinx index in the following schema:
And you can create a class to build the following ThinkingSphinx query to handle all needed associations and attributes:
You can see above a Thinking Sphinx search query with :with and :with_all hashes included. There is also an important Sphinx's @geodist function called. I hope you have readable example and the best reference for Thinking Sphinx gem you can find here:
I hope you enjoy reading my example and very clear Thinking Sphinx reference.