1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39403 from kamipo/merge_should_not_duplicate_same_clauses

Deduplicate same clauses in `merge`
This commit is contained in:
Ryuta Kamizono 2020-05-24 02:44:54 +09:00 committed by GitHub
commit 7520516605
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -24,7 +24,7 @@ module ActiveRecord
predicates_unreferenced_by(other)
end
WhereClause.new(predicates + other.predicates)
WhereClause.new(predicates | other.predicates)
end
def except(*columns)

View file

@ -98,6 +98,23 @@ class RelationMergingTest < ActiveRecord::TestCase
assert_equal [], Author.where(id: mary).merge(non_mary_and_bob)
end
def test_merge_doesnt_duplicate_same_clauses
david, mary, bob = authors(:david, :mary, :bob)
non_mary_and_bob = Author.where.not(id: [mary, bob])
author_id = Author.connection.quote_table_name("authors.id")
assert_sql(/WHERE #{Regexp.escape(author_id)} NOT IN \((\?|\W?\w?\d), \g<1>\)\z/) do
assert_equal [david], non_mary_and_bob.merge(non_mary_and_bob)
end
only_david = Author.where("#{author_id} IN (?)", david)
assert_sql(/WHERE \(#{Regexp.escape(author_id)} IN \(1\)\)\z/) do
assert_equal [david], only_david.merge(only_david)
end
end
def test_relation_merging
devs = Developer.where("salary >= 80000").merge(Developer.limit(2)).merge(Developer.order("id ASC").where("id < 3"))
assert_equal [developers(:david), developers(:jamis)], devs.to_a