Skip to content

Instantly share code, notes, and snippets.

@philsmy
Created August 18, 2011 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philsmy/1153792 to your computer and use it in GitHub Desktop.
Save philsmy/1153792 to your computer and use it in GitHub Desktop.
or scopes - add this to your model to be able to or scopes together (eg: Model.or(Model.scope1, Model.scope2). Stolen and adapted from fake_arel
__or_fn = lambda do |*scopes|
where = []
joins = []
includes = []
# for some reason, flatten is actually executing the scope
scopes = scopes[0] if scopes.size == 1
scopes.each do |s|
w = []
s.where_clauses.each do |where_clause|
w << where_clause
end
where << w.join(" AND ")
joins << s.joins_values if s.joins_values.any?
includes << s.includes_values if s.includes_values.any?
end
scoped = self
p self
scoped = scoped.includes(includes.uniq.flatten) if includes.any?
scoped = scoped.joins(joins.uniq.flatten) if joins.any?
p where.join(" OR ")
scoped.where(where.join(" OR "))
end
scope :or, __or_fn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment