Sha256: 321d10fcd578016c149a8eb66ab31e32eae8676ce6d847b48ad046b9612950dd

Contents?: true

Size: 849 Bytes

Versions: 3

Compression:

Stored size: 849 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

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

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

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

    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

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-setops-1.0.1 lib/active_record/setops.rb
activerecord-setops-1.0.0 lib/active_record/setops.rb
activerecord-setops-0.1.7 lib/active_record/setops.rb