Ruby Sequel: Array returned by query is being returned as a String object, not an Array object

狂风中的少年 提交于 2019-12-04 04:42:52

问题


I'm using the pg_array extension of Ruby Sequel.

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

And the migration to add the columns included this:

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

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

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

回答1:


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.



来源:https://stackoverflow.com/questions/45110014/ruby-sequel-array-returned-by-query-is-being-returned-as-a-string-object-not-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!