Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex Scope, Rails 3

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
like image 784
goddamnyouryan Avatar asked Aug 27 '11 22:08

goddamnyouryan


2 Answers

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
like image 146
Deradon Avatar answered Sep 25 '22 19:09

Deradon


Just from a scope point of view, you could move that logic into a scope fairly easily just by passing it to a proc.

class User
  scope :browse_for, lambda { |user|
      user.looking_for_men == true && user.looking_for_women == true
      ...
  }
end

@users = User.browse_for(@single_male)

and you could also chain scopes together to clean up the logic: http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html.

I'm not sure if that quite answers your question?

like image 23
Kristian PD Avatar answered Sep 24 '22 19:09

Kristian PD