4

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'
2
  • Please edit your question to include the code where you configure Sequel to use the pg_array extension for this column. Commented Jul 14, 2017 at 19:23
  • @JordanRunning Is that what you were looking for? Commented Jul 14, 2017 at 19:36

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thank you. Just to confirm: DB = Sequel::Model.db right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.