Mu-An Chiou
Mu-An Chiou

Reputation: 113

How to scope with "where", "having" and "or"

I understand it is possible to do

scope :public_visible, where("(status = ?) OR (end_date > ?)", :published, Date.today)

But what I want to do is combining the following two scopes with OR

scope :succeeded, having("SUM(orders.sum) >= goal")
scope :ongoing, where("end_date >= ?", Date.today)

Is this possible? Either in sql or activerecord ways.

Thanks everyone.

Upvotes: 2

Views: 114

Answers (2)

Tomanow
Tomanow

Reputation: 7367

It is better to use ARel (link) in my honest opinion. You would have something like:

def succeeded_or_ongoing
  where("id in (?) or id in (?)", Project.succeeded.map(&:id), Project.ongoing.map(&:id))
end

Then do Project.succeeded_or_ongoing

Upvotes: 0

Mu-An Chiou
Mu-An Chiou

Reputation: 113

Not a perfect solution but I ended up doing

scope :succeeded_or_ongoing, where("id in (?) or id in (?)", Project.succeeded.map(&:id), Project.ongoing.map(&:id))

Upvotes: 1

Related Questions