Having a bit of an issue with country select.

**What I'm trying to do:**

I'm trying to add an extra filter to my /browser page to allow users
to filter results by a country from a select drop down menu also.

**So far filtering by:**

texted typed in location <br />
gender <br />
ethnicity <br />
marital status <br />
sexual preference <br />

all work fine.

Filtering works for attributes that are/were stored as integers. Take
a look at my view form and you'll see I have a few select menus and
these have 'Strings' that are represented by integer values:



 **My view:**



      <%= form_tag browsers_path, :method => 'get' do %>
          <p>
            Location: <%= text_field_tag :location, params[:location]
%>
        <br />
            Country: <%= country_select(:profile, :country, [ "United
Kingdom", "France", "Germany" ], :prompt => "Select Country") %>
        <br />
            Gender: <%= select_tag :gender,
                       options_for_select([["Select", nil],
                          ["Male", 1],
                         ["Female", 2]], params[:gender]) %>
        <br />
            Ethnicity: <%= select_tag :ethnicity,
                       options_for_select([["Select", nil],['Black',
1 ],['White / Caucasian', 2 ],['European', 3 ],['Asian', 4 ],
['Indian', 5 ],['Middle Eastern', 6 ],['Native American', 7 ],
['Hispanic', 8 ],['Mixed Race', 9 ],['Other Ethnicity', 10 ]],
params[:ethnicity]) %>
        <br />

            Marital status: <%= select_tag :marital_status,
                       options_for_select([[' Select', nil],['Single',
1 ],['Dating', 2 ],['In relationship', 3 ],['Married', 4 ],['Living
Together', 5 ],['Divorced', 6 ],['Separated', 7 ],['Widowed', 8 ]],
params[:marital_status]) %>
        <br />
            Sexual preference: <%= select_tag :sexual_preference,
                       options_for_select([[' Select', nil],
['Straight', 1 ],['Gay', 2 ],['Bi-sexual', 3 ]],
params[:sexual_preference]) %>
        <br />
            <%= submit_tag "Search", :name => nil %>
          </p>
          <% end %>

Everything works fine except for country select because it is stored
as a string and has no integer value. For thinking sphinx attributes I
think they have to be integers.. well from what I read anyway.

Anyway a fix for would be to create a country select list containing
an array of countries with values and then use a select tag in the way
I've done with gender, marital status etc. This would take some time
though and I would have to make modifications to previous work and
populate my db with sample data again.

**1)** I'm wondering if theres a quick way to do what I'm trying to do
working with what I have now?

**2)** Should I have been storing countries as integers rather than
strings in the first place?
what I'm asking is if that would be better practice seeing as less
space would be taken up in the database using integers rather than
strings..

**3)** country_select seems to want a model to talk to "profile". This
was fine when I was working on profile with my profile model updating
the db, with things a user had to fill out their profile but now this
is not needed.


My model:

        class Profile < ActiveRecord::Base

          belongs_to :user
          belongs_to :photo

          # thinking sphinx
          define_index do

            indexes location
            indexes country
    has gender
    has ethnicity
    has marital_status
    has sexual_preference
    has birthday

          end

My controller:


    class BrowsersController < ApplicationController
      def index
        @default_image = "/assets/default_avatar.jpg"
        filters = {} # creates empty hash to pass in params.. i think
any way
        filters[:gender]            = params[:gender].to_i if
params[:gender].present? # self explanitory
        filters[:ethnicity]         = params[:ethnicity].to_i if
params[:ethnicity].present? # self explanitory
        filters[:marital_status]    = params[:marital_status].to_i if
params[:marital_status].present? # self explanitory
        filters[:sexual_preference] = params[:sexual_preference].to_i
if params[:sexual_preference].present? # self explanitory
        #filters[:birthday]          =
(params[:birthday].to_time.to_i..Time.now.to_i)


        @users = Profile.search params[:country],  # search with params
of location field in form
                                       :page => params[:page], # will
paginate setting
                                       :per_page => 20, # will paginate
setting
                                       :conditions_all => { :country =>
params[:country]},
                                       :with => filters

      end
    end


Kind regards

-- 
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.

Reply via email to