Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/georgetaveras1231/easy_union_set

easily create unions and intersections in ActiveRecord.
https://github.com/georgetaveras1231/easy_union_set

Last synced: about 2 months ago
JSON representation

easily create unions and intersections in ActiveRecord.

Awesome Lists containing this project

README

        

[easy_union_set](https://rubygems.org/gems/easy_union_set)
==============

easily create unions and intersections in ActiveRecord.

Setup

```ruby
gem 'easy_union_set'
```

Easy to use, So I'll jump straight into the examples

to create a UNION
```ruby
Project.where("title LIKE '%a%'") | Project.having("LENGTH(description) > 10").group(:id)

# => SELECT "projects".* FROM ( SELECT "projects".* FROM "projects" WHERE (title LIKE '%a%') UNION SELECT "projects".* FROM "projects" GROUP BY id HAVING LENGTH(description) > 10 ) "projects"
```

to create an INTERSECT
```ruby
Project.where("title LIKE '%a%'") & Project.having("LENGTH(description) > 10").group(:id)

# => SELECT "projects".* FROM ( SELECT "projects".* FROM "projects" WHERE (title LIKE '%a%') INTERSECT SELECT "projects".* FROM "projects" GROUP BY id HAVING LENGTH(description) > 10 ) "projects"
```

to create a UNION ALL
```ruby
Project.where("title LIKE '%a%'") | {:all => Project.having("LENGTH(description) > 10").group(:id)}

# => SELECT "projects".* FROM ( SELECT "projects".* FROM "projects" WHERE (title LIKE '%a%') UNION ALL SELECT "projects".* FROM "projects" GROUP BY id HAVING LENGTH(description) > 10 ) "projects"
```

if for some reason you'd still rather make multiple queries and make a ruby union simply call #to_a on the AR:Relation object before using the #& or #| methods.