Last active
December 30, 2015 04:49
-
-
Save wgrrrr/7778913 to your computer and use it in GitHub Desktop.
Take an Array of ActiveRecord::Relation objects, extract their WHERE clauses and join them via a SQL OR operator. Allows you to conditionally build the relations based on business rules.
This file contains 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
def merge_where_clause_with_or(relations) | |
relations.map { |relation| relation.arel.where_clauses }.join(' OR ') | |
end | |
# Usage | |
model.where(merge_where_clause_with_or(array_of_relations)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment