2011-03-09 240 views
0

我有兩個類似的查詢,我該如何幹活呢?兩個查詢之間只有一個條件不同。幹活動記錄查詢

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 

回答

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