Ruby Sequel: Array returned by query is being retu

2020-04-12 09:37发布

问题:

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.