ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Custom Predicate generates extra quote

Open talalong opened this issue 10 months ago • 4 comments

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

talalong avatar Mar 25 '25 13:03 talalong

Can you add a PR with a failing test please @talalong ?

scarroll32 avatar Sep 24 '25 23:09 scarroll32

@scarroll32 Do you still need the PR from me? I see you've asked the Copilot to do it :)

talalong avatar Sep 26 '25 19:09 talalong

@scarroll32 Do you still need the PR from me? I see you've asked the Copilot to do it :)

Hi @talalong I'd much prefer to have one from you if you have the time?

scarroll32 avatar Sep 27 '25 08:09 scarroll32

@scarroll32 ok my bad. I though I could resolve my issue by removing the join but I actually need it. And I am not sure on how to make a PR, since I dont have the permission to push a new branch. but here is the test, which failed as expected:

context "defining custom predicates IN" do
      before do
        Ransack.configure { |c| c.add_predicate "in_list", arel_predicate: "in", formatter: proc { |v| v&.split(';').join(',') } }
      end
      it 'generates IN query' do
        @s.name_in_list = "a;b"
        field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
        expect(@s.result.to_sql).to match /#{field} IN \('a', 'b'\)/
      end
    end

talalong avatar Sep 27 '25 22:09 talalong