aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-07-31 09:21:43 -0400
committerGitHub <noreply@github.com>2017-07-31 09:21:43 -0400
commit0f245882c9c8cdf15dcbb958680f7f2ea4f7d76a (patch)
tree5741d22c7ecc683b48f1cb435d7744f88be62557 /activerecord/lib
parent090eaa7e1b42143ffdb42409aa5d429cbeb3e55d (diff)
parentbe81b5066074fee8126144d072c6132b93d1fe39 (diff)
downloadrails-0f245882c9c8cdf15dcbb958680f7f2ea4f7d76a.tar.gz
rails-0f245882c9c8cdf15dcbb958680f7f2ea4f7d76a.tar.bz2
rails-0f245882c9c8cdf15dcbb958680f7f2ea4f7d76a.zip
Merge pull request #29950 from MaxLap/avoid_or_clause_duplicates
Avoid duplicate clauses when using #or
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/where_clause.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb
index ef2bca9155..752bb38481 100644
--- a/activerecord/lib/active_record/relation/where_clause.rb
+++ b/activerecord/lib/active_record/relation/where_clause.rb
@@ -15,6 +15,12 @@ module ActiveRecord
)
end
+ def -(other)
+ WhereClause.new(
+ predicates - other.predicates,
+ )
+ end
+
def merge(other)
WhereClause.new(
predicates_unreferenced_by(other) + other.predicates,
@@ -26,14 +32,17 @@ module ActiveRecord
end
def or(other)
- if empty?
- self
- elsif other.empty?
- other
+ left = self - other
+ common = self - left
+ right = other - common
+
+ if left.empty? || right.empty?
+ common
else
- WhereClause.new(
- [ast.or(other.ast)],
+ or_clause = WhereClause.new(
+ [left.ast.or(right.ast)],
)
+ common + or_clause
end
end