Skip to content

Fix ActiveRecord::normalizes breaking LIKE predicates wildcards#1587

Merged
scarroll32 merged 5 commits intomainfrom
copilot/fix-06f21039-81c0-4152-ae8c-070a556a38cc
Sep 24, 2025
Merged

Fix ActiveRecord::normalizes breaking LIKE predicates wildcards#1587
scarroll32 merged 5 commits intomainfrom
copilot/fix-06f21039-81c0-4152-ae8c-070a556a38cc

Conversation

Copy link
Contributor

Copilot AI commented Sep 24, 2025

Fixes an issue where ActiveRecord::normalizes (introduced in Rails 7.1) was interfering with LIKE wildcard patterns in Ransack searches, causing incorrect SQL generation.

Problem

When using ActiveRecord::normalizes on a model attribute, Ransack's LIKE-based predicates (cont, i_cont, start, end, etc.) were having their wildcard patterns normalized, breaking the search functionality:

class User < ActiveRecord::Base
  normalizes :name, with: ->(name) { name.gsub(/[^a-z0-9]/, '_') }
end

User.ransack({ name_cont: 'foo' }).result.to_sql
# Before fix: "SELECT * FROM users WHERE name LIKE '_foo_'"
# After fix:  "SELECT * FROM users WHERE name LIKE '%foo%'"

The issue occurred because ActiveRecord's normalization was being applied to values that Ransack had already formatted with LIKE wildcards (%foo%_foo_).

Solution

The fix uses Arel::Nodes.build_quoted to wrap formatted values for LIKE predicates, which bypasses ActiveRecord's normalization while preserving it for other predicate types:

  • LIKE predicates (cont, start, end, etc.): Wildcards are preserved, normalization bypassed
  • Other predicates (eq, gt, etc.): Normalization still applies to search values as expected

Changes

  • Modified lib/ransack/nodes/condition.rb to detect LIKE predicates and wrap their values appropriately
  • Added comprehensive test coverage for Rails 7.1+ with ActiveRecord::normalizes
  • All existing functionality remains unchanged

Testing

The fix includes tests that verify:

  • All LIKE predicates work correctly with normalized attributes
  • Wildcard patterns are preserved in generated SQL
  • Search functionality works end-to-end
  • Other predicates continue to normalize search values properly
  • Edge cases (empty values, complex patterns, case insensitivity) are handled

Resolves the issue described in the original bug report where % characters in LIKE patterns were being converted to _ by normalization.


Additional instructions:

ensure the specs are added as per last comment

Fixes #1488


💡 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 AI changed the title [WIP] [BUG][RoR 7.1] ActiveRecord::normalizes can break cont predicate Fix ActiveRecord::normalizes breaking LIKE predicates wildcards Sep 24, 2025
Copilot AI requested a review from scarroll32 September 24, 2025 14:09
@scarroll32 scarroll32 marked this pull request as ready for review September 24, 2025 16:38
@scarroll32 scarroll32 merged commit a391edc into main Sep 24, 2025
25 checks passed
@scarroll32 scarroll32 deleted the copilot/fix-06f21039-81c0-4152-ae8c-070a556a38cc branch September 24, 2025 16:38
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.

[BUG][RoR 7.1] ActiveRecord::normalizes can break cont predicate

2 participants