EugZol/where_exists

Bug in sub! in loop_nested_association

sprileyMSTS opened this issue · 1 comments

When using the recently added loop_nested_association code, an issue is encountered if one of the subqueries generated by a nested call to loop_nested_association returns a string like "EXISTS ( ... customer_name = 'foo\')". When this is passed in as a variable to the second argument of sub! the \' is treated as regex post-match string and is removed from the query, generating an invalid query string (at least for Oracle).

This can be avoided by using the block form of sub, which does not honor the special regex capture strings. See: https://stackoverflow.com/questions/1542214/weird-backslash-substitution-in-ruby

As a solution, we applied the following patch:

diff --git a/lib/where_exists.rb b/lib/where_exists.rb
index 059dabc..a953cfe 100644
--- a/lib/where_exists.rb
+++ b/lib/where_exists.rb
@@ -192,13 +192,13 @@ module WhereExists

     if next_association[:next_association] && next_association[:next_association][:association]
       subq = str.match(/\([^\(\)]+\)/mi)[0]
-      str.sub!(subq,
+      str.sub!(subq) do
         "(#{subq} AND (#{loop_nested_association(
           next_association[:association],
           next_association[:next_association],
           true
         )}))"
-      )
+      end
     end

     nested ? str : [query.where(str)]

Thank you, I've made a corresponding commit: ff3cd0b