How do I use an Array (of Strings, or Integers) as bind parameter in select_all or exec_query for in where clauses?
# imagine a long a complicated SQL statement
# containing many CTEs and
sql = <<~SQL
with things as (
select ....
where
x in ($1)
), morethings as (
select
)
select
sum(...),
avg(...)
from morethings
where categories in ($2)
SQL
binds = ??
ActiveRecord::Base.connection.exec_query(
sql,
"query name",
binds
).to_a
I can generate binds for scalars like this:
ActiveRecord::Relation::QueryAttribute.new("name", "Pascal", ActiveRecord::Type::String.new)
but don't know how to supply array values and it seems I am missing something.
Background: I want to run some raw SQL queries in a Rails project with the parameters being properly escaped. DB is Postgresql but I assume there is a DB agnostic way? Or do I have to use the raw connection.
The query is not related to a model and translating to Arel is possible but does not really fit the workflow.