Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

Problem

Empty string values in predicates were being ignored during search condition building, causing SQL conditions to be missing from generated queries. This behavior was inconsistent with the documented behavior that only nil values should be ignored.

For example, the following search parameters:

{
  "nameIn" => ["", "John"],
  "addressIn" => ["123 Fake St."]
}

Would generate SQL missing the empty string condition:

SELECT `users`.* FROM `users` WHERE (`users`.`address` IN ('123 Fake St.'));

Instead of the expected:

SELECT `users`.* FROM `users` WHERE (`users`.`address` IN ('123 Fake St.') AND `users`.`name` IN ('', 'John'));

Root Cause

The issue had two components:

  1. Search parameter filtering: The Search#initialize method was filtering out parameters containing empty strings using i.blank? && i != false, which treats empty strings as "blank" and removes them.

  2. Predicate validation: The default predicate validator was rejecting empty strings with the logic v.respond_to?(:empty?) ? !v.empty? : !v.nil?.

Solution

Modified Search Parameter Filtering

Updated the filtering logic in lib/ransack/search.rb to preserve strings while still filtering out truly empty values:

# Before
params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } }

# After  
params.delete_if { |k, v| 
  [*v].all? do |i|
    i.nil? || 
    (i.respond_to?(:blank?) && i.blank? && !i.is_a?(String) && i != false)
  end
}

Modified Predicate Validator

Updated the default validator in lib/ransack/predicate.rb to accept strings:

# Before
lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? }

# After
lambda { |v| v.is_a?(String) || (v.respond_to?(:empty?) ? !v.empty? : !v.nil?) }

Impact

  • ✅ Empty strings in predicates are now preserved and generate proper SQL conditions
  • ✅ Both eq and in predicates work with empty strings: name_eq: "" and name_in: ["", "John"]
  • ✅ Nil values, empty arrays, and other blank values are still filtered out as expected
  • ✅ False values and numeric zeros continue to be preserved
  • ✅ All existing functionality remains intact

Testing

Added comprehensive tests covering:

  • Empty strings in eq predicates
  • Empty strings in in predicates with mixed values
  • Preservation of other value types (false, zero, regular strings)
  • Continued filtering of nil values and empty collections
  • Updated existing tests that previously expected empty strings to be filtered

The fix resolves the specific issue while maintaining backward compatibility for all other use cases.

Fixes #722


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] predicate containing empty string results in ignored query clause Fix empty string predicate filtering - allow empty strings in search conditions Sep 24, 2025
Copilot finished work on behalf of scarroll32 September 24, 2025 14:12
@Copilot Copilot AI requested a review from scarroll32 September 24, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

predicate containing empty string results in ignored query clause
2 participants