Last active
February 7, 2018 17:53
-
-
Save lsiden/260167a4d3574a580d97 to your computer and use it in GitHub Desktop.
Revisions
-
lsiden revised this gist
Jul 25, 2014 . 1 changed file with 4 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,15 +9,11 @@ def self.included(base) module ClassMethods def union_scope(*scopes) id_column = "#{table_name}.id" if (sub_query = scopes.reject { |sc| sc.count == 0 }.map { |s| s.select(id_column).to_sql }.join(" UNION ")).present? where "#{id_column} IN (#{sub_query})" else none end end end -
lsiden revised this gist
Jul 24, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,8 @@ def self.included(base) module ClassMethods def union_scope(*scopes) id_column = "#{table_name}.id" where case scopes.reduce(0) { |c, s| c + s.count } when 0 "1 = 0" when 1 -
lsiden created this gist
Jul 21, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ #https://gist.github.com/tlowrimore/5162327 #http://stackoverflow.com/a/15413611/270511 module ActiveRecord::UnionScope def self.included(base) base.send :extend, ClassMethods end module ClassMethods def union_scope(*scopes) id_column = "#{table_name}.id" ar_queries = scopes.map { |s| s.select(id_column) }.flatten where case ar_queries.length when 0 "1 = 0" when 1 scopes[0].to_sql else sub_query = scopes.map { |s| s.select(id_column).to_sql }.join(" UNION ") "#{id_column} IN (#{sub_query})" end end end end