I want to pass a ruby array value like so:
sql = "SELECT $1"
User.connection.raw_connection.exec_params(sql, [[1,2]])
This returns
PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1
If I change sql to "SELECT $1::int[]" I get PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "[1, 2]".
Is there a way to pass a ruby array into exec_params and have it transformed into a PostgreSQL array?
You can use the encoders to do that:
raw_connection.exec(
"select $1::int[]",
[PG::TextEncoder::Array.new.encode([1, 2])]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With