Custom Predicate generates extra quote
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
Can you add a PR with a failing test please @talalong ?
@scarroll32 Do you still need the PR from me? I see you've asked the Copilot to do it :)
@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 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