如何使用 Arel::Nodes::NamedFunction 通过 SEPARATOR 选项定义 GROUP_CONCAT

How to use Arel::Nodes::NamedFunction to define GROUP_CONCAT with SEPARATOR option

我为 MariaDB 的 GROUP_CONCAT

定义了一个命名的 Arel 函数
ta = Arel::Table.new 'authors'

Arel::Nodes::NamedFunction.new( 'GROUP_CONCAT',  [ta[:name]] ).to_sql

这会生成:

"GROUP_CONCAT(`authors`.`name`)"

不过我喜欢定义分隔符选项以生成以下输出:

"GROUP_CONCAT(`authors`.`name` SEPARATOR '|' )"

我尝试了很多方法,例如

Arel::Nodes::NamedFunction.new( 'GROUP_CONCAT',  [ ta[:name]], "SEPERATOR |"  ).to_sql

不起作用,因为它创建了以下 SQL 片段:

GROUP_CONCAT(`authors`.`name`) AS SEPERATOR '|'

我需要如何指定所需输出的参数?

非常感谢:)

你差一点就成功了。所有参数都进入数组(第二个参数)。据我所知,Arel 一直被认为是私有的 API,因此文档非常稀少。

(https://github.com/rails/rails/blob/354f1c28e81d9846fb9e5346fcca50cf303c12c1/activerecord/lib/arel/nodes/named_function.rb)

Arel::Nodes::NamedFunction.new('GROUP_CONCAT', [ta[:name], Arel.sql("SEPARATOR '|'")]).to_sql