Ruby Sequel:查询返回的数组作为String对象而不是Array对象返回

编程入门 行业动态 更新时间:2024-10-28 14:33:13
本文介绍了Ruby Sequel:查询返回的数组作为String对象而不是Array对象返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 pg_array 扩展Ruby Sequel。

I'm using the pg_array extension of Ruby Sequel.

当我选择一个列为Postgresql数组时,结果是Ruby中的字符串。如何使它成为一个Ruby数组,以便可以在其上使用.each之类的东西?

When I select a column that is a Postgresql array, the result is a string in Ruby. How do I get this to be a Ruby array so I can use things like .each on it?

CaseTypeCategory.first(category_name: 'Subscription')[:values] => "{value_one,value_two}" CaseTypeCategory.first(category_name: 'Subscription')[:values][0] => "{"

我们的数据库配置包括:

Our database config includes:

Sequel.extension :pg_array, :pg_inet, :pg_json

添加列的迁移包括:

alter_table :case_type_categories do add_column :values, "text[]" end

我可以编写原始SQL来访问数组中的单个元素:

I can write raw SQL to access single elements in the array:

select values[1] from case_type_categories where category_name = 'Subscription'

推荐答案

您需要使用 DB.extension:pg_array,:pg_inet,:pg_json ,而不是 Sequel.extension:pg_array,:pg_inet,:pg_json 。否则,您只需要文件而无需修改Sequel :: Database实例的配置。

You need to use DB.extension :pg_array, :pg_inet, :pg_json, not Sequel.extension :pg_array, :pg_inet, :pg_json. Otherwise you are just requiring the files without modifying the configuration for the Sequel::Database instance.

更多推荐

Ruby Sequel:查询返回的数组作为String对象而不是Array对象返回

本文发布于:2023-10-28 22:39:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   数组   而不是   Ruby   Sequel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!