如何用ruby-on-rails的Ruby PG gem将数组作为exec_params的参数传递?

更新于
2026-10-09 19:21:55
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计179个文字,预计阅读时间需要1分钟。

如何用ruby-on-rails的Ruby PG gem将数组作为exec_params的参数传递?

我想传递一个像这样的ruby数组值:sql=SELECT $1 FROM users WHERE id=$2User.connection.raw_connection.exec_params(sql, [[1, 2]]) 这返回了 PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1。如果我将sql更改为 SELECT ' + + ,结果会怎样?

我想传递一个像这样的 ruby数组值:

sql = "SELECT $1" User.connection.raw_connection.exec_params(sql, [[1,2]])

这回来了

如何用ruby-on-rails的Ruby PG gem将数组作为exec_params的参数传递?

PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1

如果我将sql更改为“SELECT $1 :: int []”,我得到PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:“[1,2]”.

有没有办法将ruby数组传递给exec_params并将其转换为PostgreSQL数组?

您可以使用编码器来执行此操作:

raw_connection.exec( "select $1::int[]", [PG::TextEncoder::Array.new.encode([1, 2])] )

本文共计179个文字,预计阅读时间需要1分钟。

如何用ruby-on-rails的Ruby PG gem将数组作为exec_params的参数传递?

我想传递一个像这样的ruby数组值:sql=SELECT $1 FROM users WHERE id=$2User.connection.raw_connection.exec_params(sql, [[1, 2]]) 这返回了 PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1。如果我将sql更改为 SELECT ' + + ,结果会怎样?

我想传递一个像这样的 ruby数组值:

sql = "SELECT $1" User.connection.raw_connection.exec_params(sql, [[1,2]])

这回来了

如何用ruby-on-rails的Ruby PG gem将数组作为exec_params的参数传递?

PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1

如果我将sql更改为“SELECT $1 :: int []”,我得到PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:“[1,2]”.

有没有办法将ruby数组传递给exec_params并将其转换为PostgreSQL数组?

您可以使用编码器来执行此操作:

raw_connection.exec( "select $1::int[]", [PG::TextEncoder::Array.new.encode([1, 2])] )