Sha256: 64b0df2f28fd812992fa9fc557db6f648bab442d06ad3d0769086b523e838c5b

Contents?: true

Size: 865 Bytes

Versions: 6

Compression:

Stored size: 865 Bytes

Contents

require "active_record/setops/version"

module ActiveRecord
  class Relation
    # Performs a set theoretic union works like `Array#+` but puts the load on the database
    # and allows you to chain more relation operations.
    def union(other)
      binary_operation(Arel::Nodes::Union, other)
    end
    alias | union
    alias + union

    def union_all(other)
      binary_operation(Arel::Nodes::UnionAll, other)
    end

    def intersect(other)
      binary_operation(Arel::Nodes::Intersect, other)
    end
    alias & intersect

    def except(other)
      binary_operation(Arel::Nodes::Except, other)
    end
    alias difference except
    alias - except

    private

    def binary_operation(op_class, other)
      @klass.unscoped.from(Arel::Nodes::TableAlias.new(op_class.new(self.arel.ast, other.arel.ast), @klass.arel_table.name))
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activerecord-setops-0.1.5 lib/active_record/setops.rb
activerecord-setops-0.1.4 lib/active_record/setops.rb
activerecord-setops-0.1.3 lib/active_record/setops.rb
activerecord-setops-0.1.2 lib/active_record/setops.rb
activerecord-setops-0.1.1 lib/active_record/setops.rb
activerecord-setops-0.1.0 lib/active_record/setops.rb