Skip to content

Fix custom predicate formatter generating extra quotes in SQL - #1636

Closed
scarroll32 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-b4afa97a-27a3-4e44-b622-3a9880ea27be
Closed

scarroll32 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-b4afa97a-27a3-4e44-b622-3a9880ea27be

Conversation

Copilot AI commented Sep 25, 2025 •

Copy link
Copy Markdown
Contributor

Fixes an issue where custom predicates with formatters that return comma-separated string values generate malformed SQL with extra quotes.

Problem

When using custom predicates with formatters that transform input into comma-separated strings, the generated SQL contains extra quotes that break the syntax:

Ransack.configure do |config|
  config.add_predicate 'in_list',
    arel_predicate: 'in',
    formatter: ->(v) { v&.split(';').join("','") }
end

Table.ransack(column_in_list: 'test;othertest').result.to_sql

Expected SQL:

WHERE table.column IN ('test', 'othertest')

Actual SQL (broken):

WHERE table.column IN ('test'',''othertest')

Root Cause

The issue was in the formatted_values_for_attribute method in lib/ransack/nodes/condition.rb. When a formatter returns a string like "test','othertest", it was being treated as a single value instead of being parsed into separate array elements for IN clauses.

Solution

Modified formatted_values_for_attribute to detect when:

  1. A predicate expects an array (wants_array is true for 'in'/'not_in' predicates)
  2. The formatted value contains the comma-quote pattern "','"

In these cases, the string is split back into individual array elements using split("','").

Changes

  • lib/ransack/nodes/condition.rb: Added logic to handle comma-separated formatted strings
  • spec/ransack/predicate_spec.rb: Added test case to prevent regression

Backward Compatibility

This change maintains full backward compatibility:

  • Existing array formatters continue to work unchanged
  • Single value predicates are unaffected
  • Percentage handling for LIKE predicates preserved
  • All existing functionality remains intact

The fix only applies to the specific case of IN/NOT IN predicates with formatters that return comma-separated strings, making it a surgical fix that doesn't impact other functionality.

Original prompt

This section details on the original issue you should resolve

<issue_title>Custom Predicate generates extra quote</issue_title>
<issue_description>I have this Ransack configuration long time ago.

Ransack.configure do |config|
  config.add_predicate 'in_list',
    arel_predicate: 'in',
    formatter: ->(v) { v&.split(';').join("','") }
end

Calling:

Table.ransack(column_in_list: 'test;othertest').result.to_sql

Expected

WHERE table.column IN ('test', 'othertest')

Actual

WHERE table.column IN ('test'',''othertest')

There is an extra single quote added by Ransack or ActiveRecord when building the query. This breaks the SQL syntax.
What I can confirm is, it worked before.

Environment

Rails: 7.x
Ransack: 4.x
Ruby: 3.x</issue_description>

Comments on the Issue (you are @copilot in this section)

@scarroll32 Can you add a PR with a failing test please @talalong ?
Fixes #1553

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>
Copilot AI changed the title [WIP] Custom Predicate generates extra quote Fix custom predicate formatter generating extra quotes in SQL Sep 25, 2025
Copilot AI requested a review from scarroll32 September 25, 2025 07:45
@scarroll32

Copy link
Copy Markdown
Member

Note

Comment from Claude (Claude Code), acting on behalf of @scarroll32.

Closing as part of clearing the PR backlog ahead of 5.0.0 (#1640).

This is one of a batch of PRs opened by the Copilot coding agent in September 2025. They have aged out: main has moved on substantially since (Rails 8.1 support, the CI matrix rework in #1667, the v5.0.0 branch consolidation in #1647), and most of these either no longer apply cleanly, no longer pass, or carry specs that don't run under the project's _spec.rb pattern.

The underlying issue stays open. Nothing is being dismissed on the merits — the bug reports behind these PRs are still valid and still wanted. What's being retired is a batch of stale machine-generated branches that cost more to salvage than to redo.

If you're affected by the underlying issue, the most useful contribution is a PR with a failing spec that reproduces it against current main — see CONTRIBUTING.md.

@scarroll32 scarroll32 closed this Sep 21, 2026
@scarroll32
scarroll32 deleted the copilot/fix-b4afa97a-27a3-4e44-b622-3a9880ea27be branch October 1, 2026 21:47
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.

Custom Predicate generates extra quote

2 participants