There's a problem here: you are modifying the original relations. If you're lucky, this will fail with ```ActiveRecord::ImmutableRelation```, but this only happens if the relations have already been loaded.

The fix is simple:
```
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 1e291e6..ced097c 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -607,6 +607,7 @@ module ActiveRecord
         right = (ActiveRecord::Relation === opts) ? opts : klass.unscoped.where(opts, rest)
 
         unless left.where_values.empty? || right.where_values.empty?
+          left, right = left.dup, right.dup
           left.where_values = [left.where_ast.or(right.where_ast)]
           right.where_values = []
         end
```