Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/delonnewman/activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
https://github.com/delonnewman/activerecord-setops
activerecord activerecord-extension activerecord-queries arel query relational-algebra relational-database relational-databases ruby ruby-gem ruby-on-rails set set-operations set-theory sets sql sql-except sql-intersect sql-union sql-unionall
Last synced: 12 days ago
JSON representation
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
- Host: GitHub
- URL: https://github.com/delonnewman/activerecord-setops
- Owner: delonnewman
- License: mit
- Created: 2020-01-30T21:15:06.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-14T20:48:49.000Z (about 2 years ago)
- Last Synced: 2024-11-15T01:42:37.799Z (about 1 month ago)
- Topics: activerecord, activerecord-extension, activerecord-queries, arel, query, relational-algebra, relational-database, relational-databases, ruby, ruby-gem, ruby-on-rails, set, set-operations, set-theory, sets, sql, sql-except, sql-intersect, sql-union, sql-unionall
- Language: Ruby
- Size: 656 KB
- Stars: 21
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
![Ruby](https://github.com/delonnewman/activerecord-setops/workflows/Ruby/badge.svg)
[![Gem Version](https://badge.fury.io/rb/activerecord-setops.svg)](https://badge.fury.io/rb/activerecord-setops)
[![Contact me on Codementor](https://www.codementor.io/m-badges/delonnewman/book-session.svg)](https://www.codementor.io/@delonnewman?refer=badge)# ActiveRecord::Setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Has only been tested with Rails 5.# Synopsis
```ruby
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:
```ruby
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](spec/active_record/setops_spec.rb) with Rails 5, and is being used in production.
```ruby
module ActiveRecord
class Relation
def union(other)
binary_operation(Arel::Nodes::Union, other)
end
alias | uniondef union_all(other)
binary_operation(Arel::Nodes::UnionAll, other)
end
alias + union_alldef intersect(other)
binary_operation(Arel::Nodes::Intersect, other)
end
alias & intersectdef difference(other)
binary_operation(Arel::Nodes::Except, other)
end
alias - differenceprivate
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
- [Sequel](http://sequel.jeremyevans.net)
- [SQL Set Operations](https://en.wikipedia.org/wiki/Set_operations_(SQL))
- [active_record_union](https://github.com/brianhempel/active_record_union)