Thanks, All is working perfectly now.
On Jan 31, 1:21 am, "Pat Allan" <[email protected]> wrote: > Hi Gregory > > A hash must have unique keys - so it only accepts one key called :with. You > can add the age filter to your existing hash, though: > > filters[:age] = min_age..max_age > > -- > Pat > > On 31/01/2012, at 10:18 AM, GregoryHouse wrote: > > > > > > > > > So everything is working but the 2 :with defined are clashing.. only > > one works at a time. if I comment out one then the other works and I > > can get results shown. Right now the way things are only age filtering > > works and if I comment out the :with for :age then the other filters > > work. I need a way to combine them all. > > > If I do this: > > > :with => filters, { :age => min_age..max_age } > > or > > :with => { filters, :age => min_age..max_age } > > > Then age filtering doesn't work. > > > My full code is shown below. > > > class BrowsersController < ApplicationController > > def index > > @default_image = "/assets/default_avatar.jpg" > > filters = {} # creates empty hash to pass in params.. i think any > > way > > filters[:country] = params[:country].to_i if > > params[:country].present? # self explanitory > > 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 > > > # checks if there are params set and if there aren't then defaults > > are given. This basically returns all registered users > > if params[:min_age].present? > > min_age = params[:min_age].to_i > > else > > min_age = 1 > > end > > if params[:max_age].present? > > max_age = params[:max_age].to_i > > else > > max_age = 107 > > end > > > # error message for age range selection > > if params[:max_age].to_i < params[:min_age].to_i > > flash.now[:error] = "Max age can't be greater than min age" > > end > > > @users = Profile.search params[:location], # search with params of > > location field in form > > :page => params[:page], # will > > paginate setting > > :per_page => 20, # will paginate > > setting > > :with => filters, > > :with => { :age => > > min_age..max_age } > > > end > > end > > > On Jan 30, 9:07 pm, GregoryHouse <[email protected]> wrote: > >> Hi Pat I seem to have got this working and have tested with several > >> temp ages in which I will put the params info. Anyway this all seems > >> to work only when I define :with separately. When I try to add > >> the :age => min_age..max_age after the filters variable with a comma > >> it doesn't have any effect. I even tried putting both in a hash but > >> when I do that I get an error. > > >> I'm happy it's working but is it wrong to define :with twice? > > >> class BrowsersController < ApplicationController > >> def index > >> @default_image = "/assets/default_avatar.jpg" > >> filters = {} # creates empty hash to pass in params.. i think any > >> way > >> filters[:country] = params[:country].to_i if > >> params[:country].present? # self explanitory > >> 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 > >> min_age = 78 > >> max_age = 88 > > >> @users = Profile.search params[:location], # search with params of > >> location field in form > >> :page => params[:page], # will > >> paginate setting > >> :per_page => 20, # will paginate > >> setting > >> :with => filters, > >> :with => { :age => > >> min_age..max_age } > > >> end > >> end > > >> On Jan 30, 1:30 pm, "Pat Allan" <[email protected]> wrote:> Ages > >> are difficult, because Sphinx stores times and dates as timestamps from > >> Unix's epoch (1st Jan 1970) - so you can't store times earlier than that > >> (Sphinx has no concept of signed integers either, annoyingly, so negative > >> values won't get you anywhere). > > >>> You could calculate your own timestamp value with a different epoch, and > >>> store it as an integer - indeed, you could do it as number of days > >>> instead of number of seconds since a given point. Something to ponder, at > >>> least. > > >>> -- > >>> Pat > > >>> On 30/01/2012, at 11:52 PM, GregoryHouse wrote: > > >>>> Hi Pat, > > >>>> I've decided to rebuild my country select and add values as integers > >>>> to be stored in my db. I think that's much easier and efficient. Also > >>>> in future I'll be able to add a countries table if I need to and still > >>>> be able to use the stored integers to reference things in the > >>>> countries table. > >>>> All I have to do after this country select is done is age filtering. > >>>> That one seems tricky as I'll have to write some code to work out a > >>>> users age from their birthday then use that age in the filter some how > > >>>> On Jan 30, 12:37 pm, "Pat Allan" <[email protected]> wrote: > >>>>> Hi Gregory > > >>>>> You can search on specific fields - which may do the job for the > >>>>> country string data. The one catch there is if it's full country names, > >>>>> some countries may match as part of others (Democratic Republic of > >>>>> Congo vs Republic of Congo is one that comes to mind). It could be > >>>>> better to store and search on the country codes instead? The carmen gem > >>>>> may help with this. > > >>>>> That said, your current setup (as listed in your email) may work fine - > >>>>> but you should be using :conditions, not :conditions_all. > > >>>>> Cheers > > >>>>> -- > >>>>> Pat > > >>>>> On 30/01/2012, at 1:28 PM, GregoryHouse wrote: > > >>>>>> 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 > > ... > > read more » -- 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.
