postgresql 如何使用Rails查询jsonb字段中的嵌套数组,使其以数组形式出现

vbopmzt1  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(158)
Book.connection.execute("SELECT books.content -> 'summaries' from books INNER JOIN authors where books.author_id = authors.id")

这就变成了'[\"summary 1\", \"summary 2\",...]'(序列化的字符串数组),我如何将它们作为字符串数组输出?

irtuqstp

irtuqstp1#

要将JSONB数组转换为“常规”数组,请在Book.connection.execute语句中使用以下查询:

select array(select jsonb_array_elements_text(books.content -> 'summaries')) 
from books inner join authors on books.author_id = authors.id;

相关问题