Skip to content

Instantly share code, notes, and snippets.

@wakiki
Created August 10, 2012 09:12
Show Gist options
  • Save wakiki/3312792 to your computer and use it in GitHub Desktop.
Save wakiki/3312792 to your computer and use it in GitHub Desktop.
Rails 3 merging scope with OR
# 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
@eric1234
Copy link

Thanks for the code. Although I didn't use it directly, the basic idea helped me out greatly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment