我有很多参与者的战斗课程。我正在使用此类方法返回要投票的最后一场战斗:
def self.get_voteable_battle
#return the battle whose submissions_deadline ended less than 3 days ago
time_now = Time.now
end_of_day = time_now.end_of_day
return self.where(:submissions_deadline => end_of_day.ago(3.days)..time_now).first
end
但我也想确保至少有 2 名参与者。
我可以像这样添加另一个条件:
def self.get_voteable_battle
#return the battle whose submissions_deadline ended less than 3 days ago
time_now = Time.now
end_of_day = time_now.end_of_day
battle = self.where(:submissions_deadline => end_of_day.ago(3.days)..time_now).first
if battle && battle.participants.count > 1
return battle
else
return nil
end
end
但这需要另一个查询,对吗?有没有办法在一个查询中使用活动记录来做到这一点?