I have Thinking Sphinx setup and I was wondering how can search based on 
age? For example I should be able to enter "18-25" and it will show me all 
users that fall in that category based on their date of birth attribute 
from Users table.

I have age calculated already based on the users birthday which works 
perfect on their profile.

User.rb:

    def age
>       now = Time.now.utc.to_date
>       now.year - birthday.year - ((now.month > birthday.month || 
> (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1)
>     end



Search.rb:

 def users
    @users ||= find_users
  end
    
    private
    
    def find_users
      users = User.order(:id)
      users = users.where(gender: gender) if gender.present?
      users = users.where(zip_code: zip_code) if zip_code.present?
      users = users.where(children: children) if children.present?
      users = users.where(religion: religion) if religion.present?
      users = users.where(ethnicity: ethnicity) if ethnicity.present?
      if min_age.present? && max_age.present?
        min = [ min_age, max_age ].min
        max = [ min_age, max_age ].max
        min_date = Date.today - min.years
        max_date = Date.today - max.years
        users = users.where("birthday BETWEEN ? AND ?", max_date, min_date)
        users
      end
      users
    end
  end


Indexes:

ThinkingSphinx::Index.define :user, :with => :active_record do 
>   indexes name, :as => :user, :sortable => true 
>   indexes religion, zip_code, birthday, about_me, career, sexuality, 
> children, user_smoke, user_drink, gender, ethnicity, education, username
>   has created_at, updated_at 
> end  

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.

Reply via email to