-
-
Save bogdanRada/6742e2405c2fb231a1fdcd2ee3fdb798 to your computer and use it in GitHub Desktop.
Rails 3 merging scope with OR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# By Steve Leung | |
# [email protected] | |
class ActiveRecord::Relation | |
# temporarily hack for allowing combining scopes with OR | |
# doesn't add the join tables so need to use .includes manually | |
# ie. Jobship.includes(:job).or(Jobship.accepted, Jobship.declined).count | |
def or(*scopes) | |
clauses = *scopes.map do |relation| | |
clause = relation.arel.where_clauses.map { |clause| "(#{clause})" }.join(' AND ') | |
"(#{clause})" | |
end.join(' OR ') | |
where clauses | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment