0

I have 2 queries that are similar, how can I DRY them up? There is only 1 condition that is different between both queries.

if self.gender_target == "Both"
  return Drop.limit(180).live.where(
  :drops => {:navigation_section_id => 1}  
  ).group(:id).joins(:categories).where(
  :categories => {:id => self.categories}
  ).all
end

if self.gender_target != "Both"
  return Drop.limit(180).live.where(
  :drops => {:navigation_section_id => 1}, 
  :drops => {:gender_target => ["Both", self.gender_target]} #extra condition
  ).group(:id).joins(:categories).where(
  :categories => {:id => self.categories}
  ).all
end
divibisan
  • 11,659
  • 11
  • 40
  • 58
jdee
  • 11,612
  • 10
  • 38
  • 36

1 Answers1

0
@drops = Drop.limit(180).
              live.
              where(:drops => {:navigation_section_id => 1}).
              group(:id).
              joins(:categories).
              where(:categories => {:id => self.categories})

if self.gender_target != "Both"
  @drops = @drops.where(:drops => {:gender_target => ["Both", self.gender_target]})
end

return @drops.all
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201