Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Last active March 9, 2016 10:28
Show Gist options
  • Select an option

  • Save matthieuprat/6a48ea06de0baccb0519 to your computer and use it in GitHub Desktop.

Select an option

Save matthieuprat/6a48ea06de0baccb0519 to your computer and use it in GitHub Desktop.
# Adapted from http://thepugautomatic.com/2014/08/union-with-active-record/.
def self.union *scopes
sql = connection.unprepared_statement do
unions = scopes.map { |scope| "(#{scope.to_sql})" }.join(' UNION ')
"(#{unions}) AS #{table_name}"
end
from sql
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment