Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:57:43+00:00 2026-05-24T23:57:43+00:00

So I am building an application that matches users. User models have 3 attributes

  • 0

So I am building an application that matches users. User models have 3 attributes (that are relevant to my question anyways: gender:string, looking_for_men:boolean, looking_for_women:boolean.

currently I’ve got a method in my model like so:

def browse
  if self.looking_for_men == true && self.looking_for_women == true
    if self.sex == "Male"
      User.where("looking_for_men = ?", true)
    elsif self.sex == "Female"
      User.where("looking_for_women = ?", true)
    end
  elsif self.sex == "Male" && self.looking_for_men == true
    User.where("looking_for_men = ? AND sex = ?", true, "Male")
  elsif self.sex == "Female" && self.looking_for_women == true
    User.where("looking_for_women = ? AND sex = ?", true, "Female")
  else
    if self.sex == "Male"
      User.where("looking_for_men = ? AND sex = ?", true, "Female")
    elsif self.sex == "Female"
      User.where("looking_for_women = ? AND sex = ?", true, "Male")
    end
  end
end

This is pretty messy, as you can tell. Is there anyway to clean this up and make it into a scope, so that say for example I am a male user, and I am looking for women that it returns only women who are looking for men when I do a query like so:

@users = User.all.browse
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:57:44+00:00Added an answer on May 24, 2026 at 11:57 pm

    I would just do the code below, to make it more readable. But somehow,I’m not totally comfortable with this solution. Still lot of code:

    class User < ActiveRecord::Base
      scope :male,   where(:gender => "Male")
      scope :female, where(:gender => "Female")
      scope :looking_for_men,   where(:looking_for_men => true)
      scope :looking_for_women, where(:looking_for_women => true)
    
      def browse
        @women = @men = []
    
        @women = self.interested_females if self.looking_for_women
        @men   = self.interested_males   if self.looking_for_men
    
        @result = @women.concat(@men)
        @result.delete(self) #removes the user itself from the result-set
    
        return @result
      end
    
      def interested_females
        return User.female.looking_for_men   if self.male?
        return User.female.looking_for_women if self.female?
      end
    
      def interested_males
        return User.male.looking_for_men   if self.male?
        return User.male.looking_for_women if self.female?
      end
    
      def male?
        return (self.gender == "Male")
      end
    
      def female?
        return (self.gender == "Female")
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an application that can have multiple users from multiple accounts. For
I'm building an application that has its own custom chrome. I have turned the
Im building an application that requires extensive logging of actions of the users, payments,
I'm building an application that stores lots of data per user (possibly in gigabytes).
I am building an application that I want to have extended with modules that
We are building an application that will have clients installed in many sites. Each
I am building an application that will have web, windows and mobile (iPhone) client,
I am currently building an application that I will host and will have multi-tenants
I'm building an application that checks when a user needs to change the oil
I am building application that required some data from iPhone's Call log(read only). The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.