Sql 如何界定“范围”;其中&引用;拥有;及;或;

Sql 如何界定“范围”;其中&引用;拥有;及;或;,sql,ruby-on-rails,activerecord,Sql,Ruby On Rails,Activerecord,我知道这是可能的 scope :public_visible, where("(status = ?) OR (end_date > ?)", :published, Date.today) 但我想做的是将以下两个作用域与或组合 scope :succeeded, having("SUM(orders.sum) >= goal") scope :ongoing, where("end_date >= ?", Date.today) 这可能吗?以sql或activerecor

我知道这是可能的

scope :public_visible, where("(status = ?) OR (end_date > ?)", :published, Date.today)
但我想做的是将以下两个作用域与组合

scope :succeeded, having("SUM(orders.sum) >= goal")
scope :ongoing, where("end_date >= ?", Date.today)
这可能吗?以sql或activerecord方式


谢谢大家。

这不是一个完美的解决方案,但我最终做到了

scope :succeeded_or_ongoing, where("id in (?) or id in (?)", Project.succeeded.map(&:id), Project.ongoing.map(&:id))
在我看来,最好使用ARel()。你会有这样的想法:

def succeeded_or_ongoing
  where("id in (?) or id in (?)", Project.succeeded.map(&:id), Project.ongoing.map(&:id))
end
然后执行
项目。成功\u或\u正在进行