@@ -235,6 +235,9 @@ def formatted_values_for_attribute(attr)
235
235
val = attr . ransacker . formatter . call ( val )
236
236
end
237
237
val = predicate . format ( val )
238
+ if val . is_a? ( String ) && ( val . starts_with? ( '%' ) || val . ends_with? ( '%' ) )
239
+ val = Arel ::Nodes ::Quoted . new ( val )
240
+ end
238
241
val
239
242
end
240
243
if predicate . wants_array
@@ -288,12 +291,24 @@ def negative?
288
291
def arel_predicate
289
292
predicate = attributes . map { |attribute |
290
293
association = attribute . parent
291
- if negative? && attribute . associated_collection?
294
+ parent_table = association . table
295
+
296
+ if negative? && attribute . associated_collection? && not_nested_condition ( attribute , parent_table )
292
297
query = context . build_correlated_subquery ( association )
293
298
context . remove_association ( association )
294
- if self . predicate_name == 'not_null' && self . value
295
- query . where ( format_predicate ( attribute ) )
296
- Arel ::Nodes ::In . new ( context . primary_key , Arel . sql ( query . to_sql ) )
299
+
300
+ case self . predicate_name
301
+ when 'not_null'
302
+ if self . value
303
+ query . where ( format_predicate ( attribute ) )
304
+ Arel ::Nodes ::In . new ( context . primary_key , Arel . sql ( query . to_sql ) )
305
+ else
306
+ query . where ( format_predicate ( attribute ) . not )
307
+ Arel ::Nodes ::NotIn . new ( context . primary_key , Arel . sql ( query . to_sql ) )
308
+ end
309
+ when 'not_cont'
310
+ query . where ( attribute . attr . matches ( formatted_values_for_attribute ( attribute ) ) )
311
+ Arel ::Nodes ::NotIn . new ( context . primary_key , Arel . sql ( query . to_sql ) )
297
312
else
298
313
query . where ( format_predicate ( attribute ) . not )
299
314
Arel ::Nodes ::NotIn . new ( context . primary_key , Arel . sql ( query . to_sql ) )
@@ -304,10 +319,6 @@ def arel_predicate
304
319
} . reduce ( combinator_method )
305
320
306
321
if replace_right_node? ( predicate )
307
- # Replace right node object to plain integer value in order to avoid
308
- # ActiveModel::RangeError from Arel::Node::Casted.
309
- # The error can be ignored here because RDBMSs accept large numbers
310
- # in condition clauses.
311
322
plain_value = predicate . right . value
312
323
predicate . right = plain_value
313
324
end
@@ -317,6 +328,10 @@ def arel_predicate
317
328
318
329
private
319
330
331
+ def not_nested_condition ( attribute , parent_table )
332
+ parent_table . class != Arel ::Nodes ::TableAlias && attribute . name . starts_with? ( parent_table . name )
333
+ end
334
+
320
335
def combinator_method
321
336
combinator === Constants ::OR ? :or : :and
322
337
end
0 commit comments