Ruby Sequel:查询返回的数组作为String对象而不是Array对象返回 [英] Ruby Sequel: Array returned by query is being returned as a String object, not an Array object
问题描述
我正在使用 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对象返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!