Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

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 Copilot AI changed the title [WIP] Custom Predicate generates extra quote Fix custom predicate formatter generating extra quotes in SQL Sep 25, 2025
Copilot finished work on behalf of scarroll32 September 25, 2025 07:45
@Copilot Copilot AI requested a review from scarroll32 September 25, 2025 07:45
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