I have a following ruby array, need to convert array to support PostgresSQL array.
The array I have
ruby_array = ["9771", "9773", "9778", "9800", "9806"]
I need to convert this to following format
postgresSQL_array = {"9771", "9773", "9778", "9800", "9806"}
by using ruby
2
Answers
If you simply need to create a string in that format, you can stringify the Ruby array, removing the first and last characters and inserting it between curly braces.
If you’re using the PG gem, use
PG::TextEncoder::Array
to convert a Ruby Array to a PostgreSQL Array.If you’re using Ruby on Rails, simply pass the Array as a parameter and it will convert for you.
Note that most things you can do with PostgreSQL Arrays are better done with JSONB.