Skip to content

Instantly share code, notes, and snippets.

@wgrrrr
Last active December 30, 2015 04:49
Show Gist options
  • Save wgrrrr/7778913 to your computer and use it in GitHub Desktop.
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.
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