0

似たようなクエリが 2 つあるのですが、どうすれば DRY できますか? 両方のクエリ間で異なる条件は 1 つだけです。

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
4

1 に答える 1

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
于 2011-03-09T14:30:20.900 に答える