• Stars
    star
    21
  • Rank 1,084,038 (Top 22 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).

Ruby Gem Version Contact me on Codementor

Buy Me A Coffee

ActiveRecord::Setops

Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll). Has only been tested with Rails 5.

Synopsis

class Student < ActiveRecord::Base; end
class Employee < ActiveRecord::Base; end

(Student.select(:name, :birth_date) | Employee.select(:name, :birth_date)).where("name like John%")

Why?

Joins can be difficult to reason about in Arel (and SQL for that matter). Many joins can be replaced with set operations which are much simpler beasts, may offer performance gains, and have consistent mathematical properties. But these operations while present in Arel are missing in ActiveRecord. This module attempts to correct this lack.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-setops'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-setops

Non-Installation

If you'd like the functionality, but would prefer to avoid yet another dependency, please fill free to paste the following code into your nearest lib directory, I'm certain it's not perfect but it has been tested with Rails 5, and is being used in production.

module ActiveRecord
  class Relation
    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

See Also

More Repositories

1

invokable

Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).
Ruby
43
star
2

mini-levenshtein

Simple, fast Levenshtein distance and similarity ratio for Ruby
C
27
star
3

magbot

A CLI application for fetching media from jw.org feeds
Perl
9
star
4

zera-5

A light-weight Clojure interpreter
JavaScript
8
star
5

activerecord-pull

A simple query interface for pulling deeply nested data from records.
Ruby
4
star
6

contracts-gen

Generate data from contracts
Ruby
2
star
7

dragnet

A work-in-progress system for programmable surveys, and forms.
Ruby
2
star
8

ALJAFP

A Little Java, A Few Patterns
Java
1
star
9

piglatin

Pig Latin translator
Perl
1
star
10

cantor

Relational Algebra and Reporting
Ruby
1
star
11

atomjs

Clojure Atoms for Javascript. Shared, synchronous, independent state. A fork of https://github.com/cjohansen/js-atom.
JavaScript
1
star
12

sinatra-rest-service-auth

Ruby
1
star
13

sleepr

Stop hacking and get some sleep
Perl
1
star
14

connect4

JavaScript
1
star
15

tether

Ruby
1
star
16

tetris.js

An (incomplete) tetris clone
JavaScript
1
star
17

wonderscript-alpha

A lisp for the web
TypeScript
1
star
18

periscope

A Perl Module for viewing sites through a periscope
Perl
1
star
19

pandora-periscope

A Periscope for Pandora
Perl
1
star
20

java-posix

POSIX for Java a clone (for posterity, I have no intention of maintaining this) imported from tarball found here: http://www.bmsi.com/java/posix/index.html
Java
1
star
21

Pudu

A light-weight Moose-like object system that makes it easy to create encapsulated, immutable objects
Perl
1
star
22

Makakilo-Elementary

Ruby
1
star
23

sleepr-preferences

Preferences dialog for sleepr
Python
1
star
24

yugo

An experimental tool for converting legacy ColdFusion applications into Ruby / Rack applications.
Ruby
1
star
25

talks

1
star
26

asdf-vm.el

asdf-vm integration for Emacs
Emacs Lisp
1
star
27

kigo

Ruby is already pretty lispy let's take it the rest of the way.
Ruby
1
star
28

rubygems

A fork to add offline installation features, all changes are in offline branch
Ruby
1
star