On Tuesday, August 6, 2013 7:41:20 PM UTC-7, François Beausoleil wrote:

> Hi all! 
>
> I'm having difficulty retrieving arrays from a view. I would like to get a 
> Ruby Array from a PostgreSQL array: 
>
> $ bundle exec sequel postgres:///svanalytics_development 
> Your database is stored in DB... 
> > require "pp" 
> > require "sequel/extensions/pg_array" 
>

You don't need to require the file separately, DB.extension will handle 
that.
 

> > DB.extension :pg_array 
> > pp DB[:show_channels].filter("array_upper(channel_ids, 1) > 1").first 
> {:start_at=>2012-02-22 05:00:00 -0500, 
>  :interval=>"1 day", 
>  :market_id=>"d4c13fb0-db2c-012e-ebb6-40400fe46aa7", 
>  :show_id=>"71684e92-daec-11e0-9b8e-40402761cfca", 
>  :name=>"Canada Québec", 
>  :show_name=>"RDI Matin", 
>  :channel_ids=> 
>   
> "{71bfd2e8-daec-11e0-9b8e-40402761cfca,71bfd61c-daec-11e0-9b8e-40402761cfca}"}
>  
>
>
> Note channel_ids is a string literal of an array, instead of a plain-old 
> Ruby Array. show_channels is a view with this schema: 
>
>    Column    |           Type           | Modifiers | Storage  | 
> Description 
> -------------+--------------------------+-----------+----------+------------- 
>
>  start_at    | timestamp with time zone |           | plain    | 
>  interval    | interval                 |           | plain    | 
>  market_id   | uuid                     |           | plain    | 
>  show_id     | uuid                     |           | plain    | 
>  name        | character varying(96)    |           | extended | 
>  show_name   | character varying(96)    |           | extended | 
>  channel_ids | uuid[]                   |           | extended | 
>
> I use Ruby 1.9.3, PostgreSQL 9.1.5 and 
>
> $ bundle show | grep -E '(pg|sequel)' 
>   * pg (0.16.0) 
>   * sequel (3.48.0) 
>   * sequel_pg (1.6.8) 
>
> I suspect it's something really simple, but I'm not seeing it... The docs 
> at 
> http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/pg_array_rb.htmldo
>  say: "... integrates with Sequel's native postgres adapter, so that when 
> array fields are retrieved, they are parsed and returned as instances of 
> Sequel::Postgres::PGArray." I believe this is exactly what I'm doing, but 
> something's wrong on my end. 
>
> I tried Sequel 4.1.1, and channel_ids is still returned as a String. 
>

The uuid[] type is not handled natively, you need to register it:

 DB.register_array_type('uuid')

After you do that, things should work correctly.

The issue with registering it by default is some people want to handle 
UUIDs as strings, and others as UUID objects.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to