GithubHelp home page GithubHelp logo

brianhempel / active_record_union Goto Github PK

View Code? Open in Web Editor NEW
417.0 11.0 42.0 62 KB

UNIONs in ActiveRecord! Adds proper union and union_all methods to ActiveRecord::Relation.

License: Other

Ruby 100.00%

active_record_union's Introduction

ActiveRecordUnion

Gem Version Build Status

Use unions on ActiveRecord scopes without ugliness.

If you find yourself writing pluck(:id) and then feeding that into another query, you may be able to reduce the number of database requests by using a nested query or a UNION without writing crazy JOIN statements.

Quick usage examples:

current_user.posts.union(Post.published)
current_user.posts.union(Post.published).where(id: [6, 7])
current_user.posts.union("published_at < ?", Time.now)
user_1.posts.union(user_2.posts).union(Post.published)
user_1.posts.union_all(user_2.posts)

ActiveRecordUnion is tested against Rails 5.0, 5.1, and 5.2. It should also work on Rails 4.2. It may or may not work on Rails 4.0/4.1.

If you are using Postgres, you might alternatively check out ActiveRecordExtended which includes support for unions as well as other goodies.

Installation

Add this line to your application's Gemfile:

gem 'active_record_union'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_record_union

Usage

ActiveRecordUnion adds union and union_all methods to ActiveRecord::Relation so we can easily gather together queries on mutiple scopes.

Consider some users with posts:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user

  scope :published, -> { where("published_at < ?", Time.now) }
end

With ActiveRecordUnion, we can do:

# the current user's (draft) posts and all published posts from anyone
current_user.posts.union(Post.published)

Which is equivalent to the following SQL:

SELECT "posts".* FROM (
  SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 1
  UNION
  SELECT "posts".* FROM "posts"  WHERE (published_at < '2014-07-19 16:04:21.918366')
) "posts"

Because the union method returns another ActiveRecord::Relation, we can run further queries on the union.

current_user.posts.union(Post.published).where(id: [6, 7])
SELECT "posts".* FROM (
  SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 1
  UNION
  SELECT "posts".* FROM "posts"  WHERE (published_at < '2014-07-19 16:06:04.460771')
) "posts"  WHERE "posts"."id" IN (6, 7)

The union method can also accept anything that where does.

current_user.posts.union("published_at < ?", Time.now)
# equivalent to...
current_user.posts.union(Post.where("published_at < ?", Time.now))

We can also chain union calls to UNION more than two scopes, though the UNIONs will be nested which may not be the prettiest SQL.

user_1.posts.union(user_2.posts).union(Post.published)
# equivalent to...
[user_1.posts, user_2.posts, Post.published].inject(:union)
SELECT "posts".* FROM (
  SELECT "posts".* FROM (
    SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 1
    UNION
    SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 2
  ) "posts"
  UNION
  SELECT "posts".* FROM "posts"  WHERE (published_at < '2014-07-19 16:12:45.882648')
) "posts"

UNION ALL

By default, UNION will remove any duplicates from the result set. If you don't care about duplicates or you know that the two queries you are combining will not have duplicates, you call use UNION ALL to tell the database to skip its deduplication step. In some cases this can give significant performance improvements.

user_1.posts.union_all(user_2.posts)
SELECT "posts".* FROM (
  SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 1
  UNION ALL
  SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 2
) "posts"

Caveats

There's a couple things to be aware of when using ActiveRecordUnion:

  1. ActiveRecordUnion will raise an error if you try to UNION any relations that do any preloading/eager-loading. There's no sensible way to do the preloading in the subselects. If enough people complain, maybe, we can change ActiveRecordUnion to let the queries run anyway but without preloading any records.
  2. There's no easy way to get SQLite to allow ORDER BY in the UNION subselects. If you get a syntax error, you can either write my_relation.reorder(nil).union(other.reorder(nil)) or switch to Postgres.

Another nifty way to reduce extra queries

ActiveRecord already supports turning scopes into nested queries in WHERE clauses. The nested relation defaults to selecting id by default.

For example, if a user has_and_belongs_to_many :favorited_posts, we can quickly find which of the current user's posts are liked by a certain other user.

current_user.posts.where(id: other_user.favorited_posts)
SELECT "posts".* FROM "posts"
  WHERE "posts"."user_id" = 1
  AND "posts"."id" IN (
    SELECT "posts"."id"
      FROM "posts" INNER JOIN "user_favorited_posts" ON "posts"."id" = "user_favorited_posts"."post_id"
      WHERE "user_favorited_posts"."user_id" = 2
  )

If we want to select something other than id, we use select to specify. The following is equivalent to the above, but the query is done against the join table.

current_user.posts.where(id: UserFavoritedPost.where(user_id: other_user.id).select(:post_id))
SELECT "posts".* FROM "posts"
  WHERE "posts"."user_id" = 1
  AND "posts"."id" IN (
    SELECT "user_favorited_posts"."post_id"
      FROM "user_favorited_posts"
      WHERE "user_favorited_posts"."user_id" = 2
  )

(The above example is illustrative only. It might be better with a JOIN.)

State of the Union in ActiveRecord

Why does this gem exist?

Right now in ActiveRecord, if we call scope.union we get an Arel::Nodes::Union object instead of an ActiveRecord::Relation.

We could call to_sql on the Arel object and then use find_by_sql, but that's not super clean. Also, on Rails 4.0 and 4.1 if the original scopes included an association then the to_sql may produce a query with values that need to be bound (represented by ?s in the SQL) and we have to provide those ourselves. (E.g. user.posts.to_sql produces SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ?.) Rails 4.2's to_sql replaces the bind values before showing the SQL string and thus can more readily be used with find_by_sql. (E.g. Rails 4.2 to_sql would say WHERE "posts"."user_id" = 1 instead of WHERE "posts"."user_id" = ?.)

While ActiveRecord may eventually have the ability to cleanly perform UNIONs, it's currently stalled. If you're interested, the relevant URLs as of July 2014 are:

rails/rails#939 and rails/arel#239 and https://github.com/yakaz/rails/commit/29b8ebd187e0888d5e71b2e1e4a12334860bc76c

This is a gem not a Rails pull request because the standard of code quality for a PR is a bit higher, and we'd have to wait for the PR to be merged and relased to use UNIONs. That said, the code here is fairly clean and it may end up in a PR sometime.

Changelog

1.3.0 - January 14, 2018

  • Ready for Rails 5.2! Updates provided by @glebm.

1.2.0 - June 26, 2016

  • Ready for Rails 5.0! Updates provided by @glebm.

1.1.1 - Mar 19, 2016

1.1.0 - Mar 29, 2015 - Add UNION ALL support, courtesy of @pic.

1.0.1 - Sept 2, 2014 - Allow ORDER BY in UNION subselects for databases that support it (not SQLite).

1.0.0 - July 24, 2014 - Initial release.

License

ActiveRecordUnion is dedicated to the public domain by its author, Brian Hempel. No rights are reserved. No restrictions are placed on the use of ActiveRecordUnion. That freedom also means, of course, that no warrenty of fitness is claimed; use ActiveRecordUnion at your own risk.

This public domain dedication follows the the CC0 1.0 at https://creativecommons.org/publicdomain/zero/1.0/

Contributing

  1. Fork it ( https://github.com/brianhempel/active_record_union/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Run the tests:
  4. Install MySQL and PostgreSQL.
  5. You need to be able to connect to a local MySQL and Postgres database as the default user, so the specs can create a test_active_record_union database. From a vanilla install of MariaDB from Homebrew, this just works. For Postgres installed by Homebrew, you may need to run $ echo "create database my_computer_user_name;" | psql postgres since the initial database created by Homebrew is named "postgres" but PG defaults to connecting to a database named after your username.
  6. Run rake to test with all supported Rails versions. All needed dependencies will be installed via Bundler (gem install bundler if you happen not to have Bundler yet).
  7. Run rake test_rails_4_2 or rake test_rails_5_2 etc. to test a specific Rails version.
  8. There is also a bin/console command to load up a REPL for playing around
  9. Commit your changes (git commit -am 'Add some feature')
  10. Push to the branch (git push origin my-new-feature)
  11. Create a new Pull Request

active_record_union's People

Contributors

brianhempel avatar glebm avatar odedniv avatar oyeanuj avatar pic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

active_record_union's Issues

PG::SyntaxError: ERROR: syntax error at or near "desc"

I have a model with a column named desc.

It works fine when using pluck

Mission.where(id: 1).union(id: 2).pluck('desc')
# SELECT "missions"."desc" FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions"  [["id", 1], ["id", 2]]

But it is broken when using select

Mission.where(id: 1).union(id: 2).select('desc')
# SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions" LIMIT $3  [["id", 1], ["id", 2], ["LIMIT", 11]]

# ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "desc")
# LINE 1: SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHE...
#                 ^
# : SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) # UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions" # LIMIT $3

It seems that it fails to quote the column desc as "missions"."desc"

Environment

Rails version: 5.2.1
ruby version: 2.5.1
active_record_union: 1.3.0

union doesn't work for union of fields

I had expected that the following code:

BsRequest.where(id: BsRequestAction.where(id: [1,2]).select(:bs_request_id).union(Review.where(id: [1,2,3]).select(:bs_request_id)))

to produce the following query:

SELECT  `bs_requests`.* FROM `bs_requests` WHERE `bs_requests`.`id` IN ((SELECT `bs_request_actions`.`bs_request_id` FROM `bs_request_actions` WHERE `bs_request_actions`.`id` IN (1, 2)) UNION (SELECT `reviews`.`bs_request_id` FROM `reviews` WHERE `reviews`.`id` IN (1, 2, 3)))

Instead, it produces:

SELECT  `bs_requests`.* FROM `bs_requests` WHERE `bs_requests`.`id` IN (SELECT `bs_request_actions`.`id` FROM ( (SELECT `bs_request_actions`.`bs_request_id` FROM `bs_request_actions` WHERE `bs_request_actions`.`id` IN (1, 2)) UNION (SELECT `reviews`.`bs_request_id` FROM `reviews` WHERE `reviews`.`id` IN (1, 2, 3)) ) `bs_request_actions`)

Separate the table name from the table subquery

Hello, I noticed that when you use .union, it automatically names the subquery as the table name, however this sometimes is not what you want, specifically when you have an aggregate function like SUM, and you need to refer to the original table that its name has already used by the subquery.

So I wrote a little workaround for this, you can check it here.

I'm still not sure that's the best way to implement it, that's why I didn't ask for a pull request, but either way, I wanted you to consider it.

Fails on Rails 4.1 + PostgreSQL: bind supplies 2 parameters, but prepared statement requires 1

https://travis-ci.org/thredded/thredded/jobs/121800665

ActiveRecord::StatementInvalid:
PG::ProtocolViolation: ERROR: bind message supplies 2 parameters, but prepared statement "a253" requires 1
: SELECT "thredded_topics".* FROM ( (SELECT "thredded_topics".* FROM "thredded_topics" WHERE "thredded_topics"."messageboard_id" = $1 AND (to_tsvector('english', "thredded_topics"."title") @@ plainto_tsquery('english', 'Rando thread'))) UNION (SELECT "thredded_topics".* FROM "thredded_topics" INNER JOIN "thredded_posts" ON "thredded_posts"."postable_id" = "thredded_topics"."id" WHERE "thredded_topics"."messageboard_id" = $1 AND (to_tsvector('english', "thredded_posts"."content") @@ plainto_tsquery('english', 'Rando thread'))) ) "thredded_topics" ORDER BY "thredded_topics"."updated_at" DESC, "thredded_topics"."id" DESC LIMIT 50 OFFSET 0

Only getting this error on the specific combination of PostgreSQL + Rails 4.1. All the other builds are passing.

nil.union(something_not_nil)

I suppose it's not a bug, but maybe someone has found a workaround.

I have 6 objects initialised with 6 total different queries and I want to chain unions.

obj1.union(obj2).union(obj3) and so on

Each one can be nil, and I want to union all the results. There is no default (otherwise I could use default_obj.union(obj1) and so on).

Any ideas?

Cannot union relation with includes

I get an error "Cannot union relation with includes" running my app (key gems: activeadmin + active_record_union).
But the same code works without any errors running via console.

App:

#...
def self.all_except_busy_alien(admin_user)
        not_my_free_areas = self.free_areas
                         .joins(:property)
                         .where.not(:properties => {assigned_person_id: admin_user.id})

        all_my_areas = self.joins(:property)
                    .where(:properties => {assigned_person_id: admin_user.id})

        all_my_areas.union(not_my_free_areas)
end
#...

Console:

Loading development environment (Rails 4.2.0)

>> all_my_areas = Area.joins(:property).where(:properties => {assigned_person_id: 6})
... Area Load (174.8ms)  SELECT `areas`.* FROM ...
#<ActiveRecord::Relation


>> all_my_areas.count
115


>> not_my_free_areas = Area.free_areas.joins(:property).where.not(:properties => {assigned_person_id: 6})
... Area Load (0.4ms)  SELECT `areas`.* FROM ...
#<ActiveRecord::Relation


>> not_my_free_areas.count
65


>> r = all_my_areas.union(not_my_free_areas)
... SELECT `areas`.* FROM ((SELECT `areas`.* FROM `areas` INNER JOIN `properties` ON `properties`.`id` = `areas`.`property_id` WHERE `properties`.`assigned_person_id` = 6) UNION (SELECT `areas`.* FROM `areas` INNER JOIN `areas_astatuses` ON `areas_astatuses`.`area_id` = `areas`.`id` INNER JOIN `astatuses` ON `astatuses`.`id` = `areas_astatuses`.`astatus_id` INNER JOIN `properties` ON `properties`.`id` = `areas`.`property_id` WHERE `astatuses`.`tag` = 'free' AND (`properties`.`assigned_person_id` != 6))) `areas`;
#<ActiveRecord::Relation


>> r.count
180

Fails on Rails 5.0.0.beta3: undefined method `visitor' for ActiveRecord::Relation

https://travis-ci.org/thredded/thredded/jobs/121828657

 Failure/Error: [search_topics, search_posts].compact.reduce(:union)

 NoMethodError:
   undefined method `visitor' for #<ActiveRecord::Relation []>
 # gems/activerecord-5.0.0.beta3/lib/active_record/relation/delegation.rb:124:in `method_missing'
 # gems/activerecord-5.0.0.beta3/lib/active_record/relation/delegation.rb:94:in `method_missing'
 # gems/active_record_union-1.1.1/lib/active_record_union/active_record/relation/union.rb:31:in `set_operation'
 # gems/active_record_union-1.1.1/lib/active_record_union/active_record/relation/union.rb:11:in `union'
 # ./lib/thredded/topics_search.rb:17:in `each'
 # ./lib/thredded/topics_search.rb:17:in `reduce'
 # ./lib/thredded/topics_search.rb:17:in `search'
 # ./app/models/thredded/topic.rb:12:in `block in <class:Topic>'
 # gems/activerecord-5.0.0.beta3/lib/active_record/scoping/named.rb:159:in `instance_exec'
 # gems/activerecord-5.0.0.beta3/lib/active_record/scoping/named.rb:159:in `block (2 levels) in scope'
 # gems/activerecord-5.0.0.beta3/lib/active_record/relation.rb:351:in `scoping'
 # gems/activerecord-5.0.0.beta3/lib/active_record/scoping/named.rb:159:in `block in scope'
 # ./spec/models/thredded/topic_spec.rb:67:in `block (2 levels) in <module:Thredded>'

`union` ignored if used in a merge

Hi!

There's a very subtle issue I stumbled upon today if a union-ed scope is passed into ActiveRecord::Relation#merge. Any criteria from a union-built scope gets ignored when the relation gets merged into the scope chain.

https://github.com/rails/rails/blob/fe76a95b0d252a2d7c25e69498b720c96b243ea2/activerecord/lib/active_record/relation/spawn_methods.rb#L31-L39

https://github.com/rails/rails/blob/fe76a95b0d252a2d7c25e69498b720c96b243ea2/activerecord/lib/active_record/relation/merger.rb#L25-L46

I think this would probably be extremely hard to fix without monkey-patching pretty sensitive code in Relation::Merger, so the best advice I can see here would be to advise users to call #to_a on a union scope before passing it in to merge so that it continues to affect the query results.

A contrived example:

class Dog < ApplicationRecord
  has_many :bones
end

class Bone < ApplicationRecord
  belongs_to :dog 
end

unioned_scope = Dog.where(id: [1, 2]).union(Dog.where(id: [4, 5]))

# This will return bones for Dog #3, which isn't part of the unioned scope!
Bone.joins(:dog).merge(unioned_scope)

Eager-loading and support for includes, joins and references

Hi @brianhempel @glebm! I am coming from the Readme where you asked folks to make a noise around supporting 'eager-loading'. Is that still on the radar? In my case, almost all scopes have eager-loading and includes, joins and references, and I'd love to clean up my code using this!

FWIW, testing it so far, I keep getting the following error.

ArgumentError: Cannot union relation with includes.
	from /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/active_record_union-1.2.0/lib/active_record_union/active_record/relation/union.rb:53:in `verify_relations_for_set_operation!'
	from /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/active_record_union-1.2.0/lib/active_record_union/active_record/relation/union.rb:27:in `set_operation'
	from /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/active_record_union-1.2.0/lib/active_record_union/active_record/relation/union.rb:11:in `union'

Thank you for putting out this library!

gem does not work with SQLServer

By default, SQL Server table names are of the form 'dbo.table_name.' When this gem attempts to alias the unionized result-set, it tries to use the original table name, which SQL Server does not like because there is a '.' in it. I am not able to change my table names. Is there a way to supply my own alias? Or at least remove the 'dbo.' from the alias?

Is flat union chaining possible? Is it preferable to nested?

[user_1.posts, user_2.posts, Post.published].inject(:union)
Nested
SELECT "posts".* FROM (
  SELECT "posts".* FROM (
    SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 1
    UNION
    SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 2
  ) posts
  UNION
  SELECT "posts".* FROM "posts"  WHERE (published_at < '2014-07-19 16:12:45.882648')
) posts
Flat
SELECT "posts".* FROM (
  SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 1
  UNION
  SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" = 2
  UNION
  SELECT "posts".* FROM "posts"  WHERE (published_at < '2014-07-19 16:12:45.882648')
) posts

Flat chaining strikes me as potentially less problematic at least for some engines than deep nesting. Is it possible to achieve that in the current version? Would it make sense to add this?

Something like: Relation.union(user_1.posts, user_2.posts, Post.published)

Feature request: Test on Rails 6.0

Now Rails 6.0 is released, and it would be cool if there was

  • a Gemfile for Rails 6
  • a Travis configuration to run that
  • probably turn the Travis configuration into an explicit matrix, so that we can point at newer Ruby versions for Rails 6

Not binding polymorphic relation

Check out this code:

filtered_questions.to_sql`

=> "SELECT "questions"."id", "questions"."title", "questions"."meta", "questions"."content", "questions"."updated_at", "questions"."user_id", 'Question' AS "type" FROM "questions" INNER JOIN "taggings" ON "taggings"."tagged_id" = "questions"."id" AND "taggings"."tagged_type" = 'Question' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE "questions"."deleted_at" IS NULL AND (1=0)"

filtered_findings.to_sql

=> "SELECT "findings"."id", "findings"."title", "findings"."meta", "findings"."content", "findings"."updated_at", "findings"."user_id", 'Finding' AS "type" FROM "findings" INNER JOIN "taggings" ON "taggings"."tagged_id" = "findings"."id" AND "taggings"."tagged_type" = 'Finding' INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE "findings"."deleted_at" IS NULL AND (1=0)"

filtered_questions.union_all(filtered_findings).to_sql

=> "SELECT "questions".* FROM ( (SELECT "questions"."id", "questions"."title", "questions"."meta", "questions"."content", "questions"."updated_at", "questions"."user_id", 'Question' AS "type" FROM "questions" INNER JOIN "taggings" ON "taggings"."tagged_id" = "questions"."id" AND "taggings"."tagged_type" = INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE "questions"."deleted_at" IS NULL AND (1=0)) UNION ALL (SELECT "findings"."id", "findings"."title", "findings"."meta", "findings"."content", "findings"."updated_at", "findings"."user_id", 'Finding' AS "type" FROM "findings" INNER JOIN "taggings" ON "taggings"."tagged_id" = "findings"."id" AND "taggings"."tagged_type" = INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE "findings"."deleted_at" IS NULL AND (1=0)) ) questions"

AND \"taggings\".\"tagged_type\" = INNER JOIN <- error spotted?

This gem somehow misses these two bindings and when I try to execute query I get error that 2 bindings are missing.

Feature request: union relations and SQL strings

I recently needed this and thought the project might want something like dis. Idea is for this to work;

User.new(id: 1).posts.union("SELECT * FROM posts WHERE created_at > now()")

I'd be happy to work on it if you think it valuable.

Test suite depends on activerecord version

it is green with activerecord 4.0.13,
1 failure with activerecord 4.0.8,
6 failures with activerecord 4.2.0 (but union apparently works without problems).

Different versions of activerecord build different SQL statement, but the test suite is comparing #to_sql to an expected SQL string.

I have no idea how this could be easily fixed.

Problem with (empty result).update_all

Example: Model User:
User.none.union(User.none).update_all(username: 'new username')

Setting username = 'new username' to all users of the database -> When User.none.union(User.none).count is equal to 0!

Rails version:
Rails 5.0.5

Ruby version:
ruby 2.5.3p105

Thanks

How can I get the UNION of 2 different tables

Hi,

All the examples I could see are for the same table. I've tried messing around to see if I could get the UNION of multiple tables but failed every time. I'm basically wanting to produce a query that looks something like:

SELECT id, updated_at FROM table_1 UNION SELECT id, updated_at FROM table_2 ORDER BY updated_at;

If I try something like this I only get results from table_1:

t1 = Table1.select(:id, :updated_at).where('updated_at > ?', 2.weeks.ago)
t2 = Table2.select(:id, :updated_at).where('updated_at > ?', 2.weeks.ago)
results = Table1.union(t1).union(t2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.