问题
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